-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuzzyTime.php
70 lines (64 loc) · 2.22 KB
/
FuzzyTime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
class FuzzyTime
{
/**
* Various time formats - used in calculations
*/
private static $_time_formats = [
[60, 'just now'],
[90, '1 minute'],
[3600, 'minutes', 60],
[5400, '1 hour'],
[86400, 'hours', 3600],
[129600, '1 day'],
[604800, 'days', 86400],
[907200, '1 week'],
[2628000, 'weeks', 604800],
[3942000, '1 month'],
[31536000, 'months', 2628000],
[47304000, '1 year'],
[3153600000, 'years', 31536000],
];
/**
* Convert date into a 'fuzzy' format: 15 minutes ago, 3 days ago, etc.
*
* @param string|number $date_from Unix timestamp or a string to parse to a date
*
* @return string Human readable relative date as string
*/
public static function getFuzzyTime($date_from)
{
$now = time(); // current unix timestamp
// if a number is passed assume it is a unix time stamp
// if string is passed try and parse it to unix time stamp
if (is_numeric($date_from)) {
$dateFrom = $date_from;
} elseif (is_string($date_from)) {
$dateFrom = strtotime($date_from);
}
// difference between now and the passed time.
$difference = $now - $dateFrom;
// value to return
$val = '';
if ($dateFrom <= 0) {
$val = 'a long time ago';
} else {
// loop through each format measurement in array
foreach (self::$_time_formats as $format) {
// if the difference from now and passed time is less than first option in format measurment
if ($difference < $format[0]) {
// if the format array item has no calculation value
if (count($format) == 2) {
$val = $format[1] . ($format[0] === 60 ? '' : ' ago');
break;
} else {
// divide difference by format item value to get number of units
$val = ceil($difference / $format[2]) . ' ' . $format[1] . ' ago';
break;
}
}
}
}
return $val;
}
}