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
| <?php
class FuzzyTime
{
/**
* Various time formats - used in calculations
*/
private static $_time_formats = array(
array(60, 'just now'),
array(90, '1 minute'),
array(3600, 'minutes', 60),
array(5400, '1 hour'),
array(86400, 'hours', 3600),
array(129600, '1 day'),
array(604800, 'days', 86400),
array(907200, '1 week'),
array(2628000, 'weeks', 604800),
array(3942000, '1 month'),
array(31536000, 'months', 2628000),
array(47304000, '1 year'),
array(3153600000, 'years', 31536000),
);
/**
* Convert date into a 'fuzzy' format
* - 15 minutes ago, 3 days ago, etc.
* Pass a unix timestamp or a string to parse to a date.
* @param string|number
* @return 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 = $now - $dateFrom;// difference between now and the passed time.
$val = '';// value to return
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;
}
}
?> |