-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjetset.php
90 lines (71 loc) · 1.88 KB
/
jetset.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
date_default_timezone_set('GMT');
function parse_locations($itinerary) {
$locations = array(
'next' => null,
'current' => null,
'last' => null,
'future' => array(),
'past' => array()
);
krsort($itinerary);
foreach($itinerary as $date => $location) {
$location = array('date' => $date, 'place' => $location);
if ($date > time()) {
$locations['future'][] = $location;
} else if (!$locations['current']) {
$locations['next'] = array_pop($locations['future']);
$locations['current'] = $location;
} else if (!$locations['last']) {
$locations['last'] = $location;
} else {
$locations['past'][] = $location;
}
}
$locations['future'] = array_reverse($locations['future']);
return $locations;
}
# Thanks for the head start, Marco
# (http://www.marco.org/46559003)
function relative_date($date) {
$seconds = time() - intval($date);
$future = $seconds < 0;
$seconds = abs($seconds);
$units = array(
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day'
);
foreach ($units as $max => $unit) {
if ($seconds < $max) continue;
$num = floor($seconds / $max);
if ($num == 1) {
if ($future) {
$format = 'next %s';
} else {
$format = 'last %s';
}
} else {
$unit = pluralize($num, $unit);
if ($future) {
$format = 'in %s';
} else {
$format = '%s ago';
}
}
$string = sprintf($format, $unit);
break;
}
if ($string == 'next day') {
$string = 'tomorrow';
} else if ($string == 'last day') {
$string = 'yesterday';
}
return $string;
}
# This is Marco's too. Same place.
function pluralize($number, $noun, $nouns = false) {
if (!$nouns) $nouns = $noun . 's';
return $number . ' ' . ($number == 1 ? $noun : $nouns);
}