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%*)

Human Readable Time Difference With PHP

Human Readable Time Difference With PHP

Most social networks provide a historical timestamp to read as a human-readable string - such as 1 day ago, 3 weeks ago, 1 year, 5 months, and so on. The same application applies to future events or posts. The purpose of the string is to provide a general frame of time-reference and thus negating the need for any mental acrobatics. WordPress provides a default function called human_time_diff() that accomplishes this from within WordPress. It's this function we've slightly modified for use in other PHP applications (outside of WP).

PHP Code

1
<?php 
2
/*
3
 Retrieve the plural or single form based on the supplied amount.
4
 @param string $single The text that will be used if $number is 1.
5
  @param string $plural The text that will be used if $number is not 1.
6
 @param int    $number The number to compare against to use either $single or $plural.
7
 @return string Either $single or $plural translated text.
8
 https://core.trac.wordpress.org/browser/tags/4.7.3/src/wp-includes/l10n.php#L361
9
*/
10
 
11
function _n($single, $plural, $number) {
12
  return 1 === $number ? $single : $plural;
13
}
14
 
15
/*
16
 
17
 Determines the difference between two timestamps.
18
 Static Day Countdown with WordPress Shortcode or PHP
19
 http://www.beliefmedia.com/static-countdown
20
 https://core.trac.wordpress.org/browser/tags/4.8/src/wp-includes/formatting.php#L3216
21
*/
22
 
23
function beliefmedia_human_time_diff($from, $to = '', $text = false) {
24
 
25
    if (empty($to)) $to = time();
26
    $diff = (int) abs($to - $from);
27
 
28
    if ($diff < 3600) {
29
      $mins = round($diff / 60);
30
      if ($mins <= 1)
31
        $mins = 1;
32
      /* translators: min=minute */
33
      $since = sprintf(_n('%s min', '%s mins', $mins), $mins);
34
    }
35
    elseif ($diff < 86400 && $diff >= 3600) {
36
      $hours = round($diff / 3600);
37
      if ($hours <= 1)
38
        $hours = 1;
39
      $since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
40
    }
41
    elseif ($diff < 604800 && $diff >= 86400) {
42
      $days = round($diff / 86400);
43
      if ($days <= 1)
44
        $days = 1;
45
      $since = sprintf(_n('%s day', '%s days', $days), $days);
46
    }
47
    elseif ($diff < 30 * 86400 && $diff >= 604800) {
48
      $weeks = round($diff / 604800);
49
      if ($weeks <= 1)
50
        $weeks = 1;
51
      $since = sprintf(_n('%s week', '%s weeks', $weeks), $weeks);
52
    }
53
    elseif ($diff < 31536000 && $diff >= 30 * 86400) {
54
      $months = round($diff / (30 * 86400));
55
      if ($months <= 1)
56
        $months = 1;
57
      $since = sprintf(_n('%s month', '%s months', $months), $months);
58
    }
59
    elseif ($diff >= 31536000) {
60
      $years = round($diff / 31536000);
61
      if ($years <= 1)
62
        $years = 1;
63
      $since = sprintf(_n('%s year', '%s years', $years), $years);
64
    }
65
 
66
 return ($to - $from > 0) ? $since : $since = ($text !== false) ? $since. ' ago' : $since;
67
}

Examples

1
<?php 
2
/* Usage */
3
$from = '1497911060'; $to = '117849600';
4
echo beliefmedia_human_time_diff($from, $to, $text = true);

Returns: 44 years ago.

1
<?php 
2
/* Usage */
3
$from = '1497911060'; $to = '1514721599'; /* NYE 2017 */
4
echo beliefmedia_human_time_diff($from, $to, $text = true);

Returns: 6 months.

Considerations

  • The argument of $text determines if the ago text is returned.
  • You may find other variations of this function on our PHP Snippets page.
  • See also: Static Day Countdown with WordPress Shortcode or PHP.
  • See also: Display Time To Published or Scheduled Posts (WP Snippet).
  • The _n function is one to essentially nullify the WP translation object.

Download


Title: Human Readable Time Difference With PHP
Description: Human Readable Time Difference With PHP. A modified version of WP's human_time_diff function.
  Download • Version 0.2, 1.1K, zip, Category: PHP Code & Snippets

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?
 

RELATED READING

Like this article?

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

Leave a comment