17 private static $locales = [
'fr' => [
'fr',
'fr_FR',
'fr_FR.UTF-8'],
'en' => [
'en',
'en_US',
'en_US.UTF-8']];
20 $locale = self::$locales [$locale] ?? self::$locales [
"en"];
21 \setlocale(LC_TIME, $locale [0], $locale [1], $locale [2]);
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);
32 return $date->format(self::MYSQL_DATE_FORMAT);
36 return $datetime->format(self::MYSQL_DATETIME_FORMAT);
39 public static function longDate($date, $locale =
'en') {
41 return \date(
'l d F Y', \strtotime($date));
44 public static function shortDate($date, $locale =
'en') {
46 return \date(
'd/m/y', \strtotime($date));
51 return \date(
'c', \strtotime($datetime));
56 return \date(
'l d F Y, H:i:s', \strtotime($datetime));
67 public static function elapsed($datetime,
bool $full =
false): string {
68 $now = new \DateTime ();
69 if (!$datetime instanceof \DateTime) {
70 $ago = new \DateTime ($datetime);
74 $diff = $now->diff($ago);
76 $weeks = \floor($diff->d / 7);
77 $diff->d -= $weeks * 7;
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' :
'');
89 $string = \array_slice($string, 0, 1);
91 return $string ? \implode(
', ', $string) . ($diff->invert ?
' ago' :
'') :
'just now';
DateTime utilities Ubiquity\utils\base$UDateTime This class is part of Ubiquity.
static mysqlDateTime(\DateTime $datetime)
static setLocale($locale)
const MYSQL_DATETIME_FORMAT
static longDatetime($datetime, $locale='en')
static shortDate($date, $locale='en')
static secondsToTime($seconds)
static mysqlDate(\DateTime $date)
static longDate($date, $locale='en')
static elapsed($datetime, bool $full=false)
static shortDatetime($datetime, $locale='en')