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