Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

Example Interest Rates: Home Loan Variable: 1.89% (1.9%*) • Home Loan Fixed: 1.84% (2.11%*) • Fixed: 1.84% (2.11%*) • Variable: 1.89% (1.9%*) • Investment IO: 2.05% (4.36%*) • Investment PI: 1.58% (2.72%*)

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.

  AUS Eastern Standard Time (Connecticut)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment