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
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
29
30
31
if ($diff < 3600) {
32
33
if ($mins <= 1)
34
$mins = 1;
35
/* translators: min=minute */
36
37
}
38
elseif ($diff < 86400 && $diff >= 3600) {
39
40
if ($hours <= 1)
41
$hours = 1;
42
43
}
44
elseif ($diff < 604800 && $diff >= 86400) {
45
46
if ($days <= 1)
47
$days = 1;
48
49
}
50
elseif ($diff < 30 * 86400 && $diff >= 604800) {
51
52
if ($weeks <= 1)
53
$weeks = 1;
54
55
}
56
elseif ($diff < 31536000 && $diff >= 30 * 86400) {
57
58
if ($months <= 1)
59
$months = 1;
60
61
}
62
elseif ($diff >= 31536000) {
63
64
if ($years <= 1)
65
$years = 1;
66
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
- The argument of
$text
determines if theago
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
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