Skip to content

Commit 99cd7ea

Browse files
committed
[utils][skip ci] fix php8.2 depreciation in UDateTime elapsed
1 parent e887881 commit 99cd7ea

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

src/Ubiquity/utils/base/UDateTime.php

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,80 +14,81 @@
1414
class UDateTime {
1515
const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s';
1616
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']];
1818

1919
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]);
2222
}
2323

2424
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);
2929
}
3030

3131
public static function mysqlDate(\DateTime $date) {
32-
return $date->format ( self::MYSQL_DATE_FORMAT );
32+
return $date->format(self::MYSQL_DATE_FORMAT);
3333
}
3434

3535
public static function mysqlDateTime(\DateTime $datetime) {
36-
return $datetime->format ( self::MYSQL_DATETIME_FORMAT );
36+
return $datetime->format(self::MYSQL_DATETIME_FORMAT);
3737
}
3838

3939
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));
4242
}
4343

4444
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));
4747
}
4848

4949
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));
5252
}
5353

5454
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));
5757
}
5858

5959
/**
6060
*
6161
* @param string|\DateTime $datetime
6262
* @param boolean $full
6363
* @return string
64+
* @throws \Exception
6465
* @see http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago
6566
*/
66-
public static function elapsed($datetime, $full = false) {
67+
public static function elapsed($datetime, bool $full = false): string {
6768
$now = new \DateTime ();
68-
if (! $datetime instanceof \DateTime) {
69-
$ago = new \DateTime ( $datetime );
69+
if (!$datetime instanceof \DateTime) {
70+
$ago = new \DateTime ($datetime);
7071
} else {
7172
$ago = $datetime;
7273
}
73-
$diff = $now->diff ( $ago );
74+
$diff = $now->diff($ago);
7475

75-
$diff->w = \floor ( $diff->d / 7 );
76-
$diff->d -= $diff->w * 7;
76+
$weeks = \floor($diff->d / 7);
77+
$diff->d -= $weeks * 7;
7778

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' : '');
8283
} else {
83-
unset ( $string [$k] );
84+
unset ($string [$k]);
8485
}
8586
}
8687

87-
if (! $full){
88-
$string = \array_slice ( $string, 0, 1 );
88+
if (!$full) {
89+
$string = \array_slice($string, 0, 1);
8990
}
90-
return $string ? \implode ( ', ', $string ) . ($diff->invert ? ' ago' : '') : 'just now';
91+
return $string ? \implode(', ', $string) . ($diff->invert ? ' ago' : '') : 'just now';
9192
}
9293
}
9394

0 commit comments

Comments
 (0)