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

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
 
12
function _n($single, $plural, $number) {
13
  return 1 === $number ? $single : $plural;
14
}
15
 
16
 
17
/*
18
 
19
    Determines the difference between two timestamps.
20
    Static Day Countdown with WordPress Shortcode or PHP
21
    http://www.beliefmedia.com/static-countdown
22
    https://core.trac.wordpress.org/browser/tags/4.8/src/wp-includes/formatting.php#L3216
23
*/
24
 
25
 
26
function beliefmedia_human_time_diff($from, $to = '', $text = false) {
27
 
28
    if (empty($to)) $to = time();
29
    $diff = (int) abs($to - $from);
30
 
31
    if ($diff < 3600) {
32
      $mins = round($diff / 60);
33
      if ($mins <= 1)
34
        $mins = 1;
35
      /* translators: min=minute */
36
      $since = sprintf(_n('%s min', '%s mins', $mins), $mins);
37
    }
38
    elseif ($diff < 86400 && $diff >= 3600) {
39
      $hours = round($diff / 3600);
40
      if ($hours <= 1)
41
        $hours = 1;
42
      $since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
43
    }
44
    elseif ($diff < 604800 && $diff >= 86400) {
45
      $days = round($diff / 86400);
46
      if ($days <= 1)
47
        $days = 1;
48
      $since = sprintf(_n('%s day', '%s days', $days), $days);
49
    }
50
    elseif ($diff < 30 * 86400 && $diff >= 604800) {
51
      $weeks = round($diff / 604800);
52
      if ($weeks <= 1)
53
        $weeks = 1;
54
      $since = sprintf(_n('%s week', '%s weeks', $weeks), $weeks);
55
    }
56
    elseif ($diff < 31536000 && $diff >= 30 * 86400) {
57
      $months = round($diff / (30 * 86400));
58
      if ($months <= 1)
59
        $months = 1;
60
      $since = sprintf(_n('%s month', '%s months', $months), $months);
61
    }
62
    elseif ($diff >= 31536000) {
63
      $years = round($diff / 31536000);
64
      if ($years <= 1)
65
        $years = 1;
66
      $since = sprintf(_n('%s year', '%s years', $years), $years);
67
    }
68
 
69
 return ($to - $from > 0) ? $since : $since = ($text !== false) ? $since. ' ago' : $since; 
70
}

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

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

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

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433