-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.php
66 lines (60 loc) · 2.09 KB
/
time.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
<?php
/*
* These functions are to mess with time.
*/
// Calculate how long ago something was:
function get_time_ago( $time_stamp ) {
$time_difference = strtotime('now') - $time_stamp;
if ( $time_difference >= 60 * 60 * 24 * 365.242199 ) {
/*
* 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year
* This means that the time difference is 1 year or more
*/
return get_time_ago_string( $time_stamp, 60 * 60 * 24 * 365.242199, 'year' );
} elseif ($time_difference >= 60 * 60 * 24 * 30.4368499) {
/*
* 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month
* This means that the time difference is 1 month or more
*/
return get_time_ago_string( $time_stamp, 60 * 60 * 24 * 30.4368499, 'month' );
} elseif ( $time_difference >= 60 * 60 * 24 * 7 ) {
/*
* 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week
* This means that the time difference is 1 week or more
*/
return get_time_ago_string( $time_stamp, 60 * 60 * 24 * 7, 'week' );
} elseif ( $time_difference >= 60 * 60 * 24 ) {
/*
* 60 seconds/minute * 60 minutes/hour * 24 hours/day
* This means that the time difference is 1 day or more
*/
return get_time_ago_string( $time_stamp, 60 * 60 * 24, 'day' );
} elseif ( $time_difference >= 60 * 60 ) {
/*
* 60 seconds/minute * 60 minutes/hour
* This means that the time difference is 1 hour or more
*/
return get_time_ago_string($time_stamp, 60 * 60, 'hour');
} else {
/*
* 60 seconds/minute
* This means that the time difference is a matter of minutes
*/
return get_time_ago_string($time_stamp, 60, 'minute');
}
}
function get_time_ago_string( $time_stamp, $divisor, $time_unit ) {
$time_difference = strtotime( 'now' ) - $time_stamp;
$time_units = floor( $time_difference / $divisor );
settype( $time_units, 'string' );
if ( $time_units === '0') {
return 'less than 1 ' . $time_unit;
} elseif ( $time_units === '1' ) {
return '1 ' . $time_unit;
} else {
/*
* More than "1" $time_unit. This is the "plural" message.
*/
return $time_units . ' ' . $time_unit . 's';
}
}