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