RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Display Time To Published or Scheduled Posts

We wrote this snippet to support our article here, titled, "Static Day Countdown with WordPress Shortcode or PHP". It's another example of how the readable time difference function WordPress provides might be used.

The function itself doesn't do much but it can be incorporated into other tools to provide a more usable feature. As an example, if we want to display the time since our first (linked) article was published, I'll use [timeago id="8724"]here[/timeago]. The result: [timeago id="8724"]here[/timeago] (my bolding). It'll either display X time ago or X time from now depending upon whether it's published or scheduled.

1
<?php 
2
/*
3
 Display Time To Published or Scheduled Posts
4
 http://www.beliefmedia.com/code/wp-snippets/display-time
5
*/
6
 
7
function beliefmedia_posted_ago($atts) {
8
 
9
  extract(shortcode_atts(array(
10
    'id' => '',
11
    'text' => 1,
12
    'offset' => false,
13
  ), $atts));
14
 
15
  $time = time();
16
  $postdate = get_post_time('U', true, $id, false);
17
 
18
  /* UTC offset */
19
  $postdate = ($offset !== false) ? (strpos($offset,'-') !== false) ? bcadd($postdate, abs((3600 * $offset))) : bcsub($postdate, abs((3600 * $offset))) : $postdate;
20
 
21
  /* Get difference */
22
  $post_date = human_time_diff($postdate);
23
 
24
 /* Return result with optional extension */
25
 return ($text) ? $post_date . $ext = ($postdate < $time) ? ' ago' : ' from now' : $post_date;
26
}
27
add_shortcode('timeago', 'beliefmedia_posted_ago');

By default, $postdate will return local time (the second argument in get_post_time() determines if a GMT or local time is returned). An $offset (defined in your shortcode) will unlikely be necessary.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment