1
|
|
|
<?php |
2
|
|
|
namespace Ubiquity\utils\base; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* DateTime utilities |
6
|
|
|
* Ubiquity\utils\base$UDateTime |
7
|
|
|
* This class is part of Ubiquity |
8
|
|
|
* |
9
|
|
|
* @author jcheron <[email protected]> |
10
|
|
|
* @version 1.0.1 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class UDateTime { |
14
|
|
|
|
15
|
|
|
const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s'; |
16
|
|
|
|
17
|
|
|
const MYSQL_DATE_FORMAT = 'Y-m-d'; |
18
|
|
|
|
19
|
5 |
|
private static $locales = [ |
20
|
5 |
|
"fr" => [ |
21
|
5 |
|
"fr", |
22
|
|
|
"fr_FR", |
23
|
|
|
"fr_FR.UTF-8" |
24
|
|
|
], |
25
|
5 |
|
"en" => [ |
26
|
5 |
|
"en", |
27
|
|
|
"en_US", |
28
|
1 |
|
"en_US.UTF-8" |
29
|
1 |
|
] |
30
|
1 |
|
]; |
31
|
1 |
|
|
32
|
1 |
|
private static function setLocale($locale) { |
33
|
|
|
if (isset(self::$locales[$locale])) { |
34
|
|
|
$locale = self::$locales[$locale]; |
35
|
1 |
|
} else { |
36
|
1 |
|
$locale = self::$locales["en"]; |
37
|
|
|
} |
38
|
|
|
setlocale(LC_TIME, $locale[0], $locale[1], $locale[2]); |
39
|
1 |
|
} |
40
|
1 |
|
|
41
|
|
|
public static function secondsToTime($seconds) { |
42
|
|
|
$hours = floor($seconds / 3600); |
43
|
|
|
$mins = floor($seconds / 60 % 60); |
44
|
|
|
$secs = floor($seconds % 60); |
45
|
|
|
return sprintf('%02d:%02d:%02d', $hours, $mins, $secs); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public static function mysqlDate(\DateTime $date) { |
49
|
1 |
|
return $date->format(self::MYSQL_DATE_FORMAT); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
public static function mysqlDateTime(\DateTime $datetime) { |
53
|
3 |
|
return $datetime->format(self::MYSQL_DATETIME_FORMAT); |
54
|
3 |
|
} |
55
|
3 |
|
|
56
|
|
|
public static function longDate($date, $locale = "en") { |
57
|
|
|
self::setLocale($locale); |
58
|
1 |
|
return utf8_encode(strftime("%A %d %B %Y", strtotime($date))); |
59
|
1 |
|
} |
60
|
1 |
|
|
61
|
|
|
public static function shortDate($date, $locale = "en") { |
62
|
|
|
self::setLocale($locale); |
63
|
|
|
return strftime("%x", strtotime($date)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function shortDatetime($datetime, $locale = "en") { |
67
|
|
|
self::setLocale($locale); |
68
|
|
|
return strftime("%c", strtotime($datetime)); |
69
|
|
|
} |
70
|
1 |
|
|
71
|
1 |
|
public static function longDatetime($datetime, $locale = "en") { |
72
|
1 |
|
self::setLocale($locale); |
73
|
1 |
|
return utf8_encode(strftime("%A %d %B %Y, %T", strtotime($datetime))); |
74
|
|
|
} |
75
|
1 |
|
|
76
|
1 |
|
/** |
77
|
|
|
* |
78
|
1 |
|
* @param string $datetime |
79
|
1 |
|
* @param boolean $full |
80
|
1 |
|
* @return string |
81
|
1 |
|
* @see http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago |
82
|
|
|
*/ |
83
|
1 |
|
public static function elapsed($datetime, $full = false) { |
84
|
|
|
$now = new \DateTime(); |
85
|
|
|
if (! $datetime instanceof \DateTime) { |
|
|
|
|
86
|
|
|
$ago = new \DateTime($datetime); |
87
|
1 |
|
} else { |
88
|
1 |
|
$ago = $datetime; |
89
|
1 |
|
} |
90
|
|
|
$diff = $now->diff($ago); |
91
|
|
|
|
92
|
|
|
$diff->w = floor($diff->d / 7); |
93
|
|
|
$diff->d -= $diff->w * 7; |
94
|
|
|
|
95
|
|
|
$string = array( |
96
|
|
|
'y' => 'year', |
97
|
|
|
'm' => 'month', |
98
|
|
|
'w' => 'week', |
99
|
|
|
'd' => 'day', |
100
|
|
|
'h' => 'hour', |
101
|
|
|
'i' => 'minute', |
102
|
|
|
's' => 'second' |
103
|
|
|
); |
104
|
|
|
foreach ($string as $k => &$v) { |
105
|
|
|
if ($diff->$k) { |
106
|
|
|
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); |
107
|
|
|
} else { |
108
|
|
|
unset($string[$k]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (! $full) |
113
|
|
|
$string = array_slice($string, 0, 1); |
114
|
|
|
return $string ? implode(', ', $string) . ($diff->invert ? ' ago' : '') : 'just now'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|