Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UDateTime.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\base;
4
14class 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 private static function setLocale($locale) {
20 $locale = self::$locales [$locale] ?? self::$locales ["en"];
21 \setlocale(LC_TIME, $locale [0], $locale [1], $locale [2]);
22 }
23
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);
29 }
30
31 public static function mysqlDate(\DateTime $date) {
32 return $date->format(self::MYSQL_DATE_FORMAT);
33 }
34
35 public static function mysqlDateTime(\DateTime $datetime) {
36 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 public static function shortDate($date, $locale = 'en') {
45 self::setLocale($locale);
46 return \date('d/m/y', \strtotime($date));
47 }
48
49 public static function shortDatetime($datetime, $locale = 'en') {
50 self::setLocale($locale);
51 return \date('c', \strtotime($datetime));
52 }
53
54 public static function longDatetime($datetime, $locale = 'en') {
55 self::setLocale($locale);
56 return \date('l d F Y, H:i:s', \strtotime($datetime));
57 }
58
67 public static function elapsed($datetime, bool $full = false): string {
68 $now = new \DateTime ();
69 if (!$datetime instanceof \DateTime) {
70 $ago = new \DateTime ($datetime);
71 } else {
72 $ago = $datetime;
73 }
74 $diff = $now->diff($ago);
75
76 $weeks = \floor($diff->d / 7);
77 $diff->d -= $weeks * 7;
78
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' : '');
83 } else {
84 unset ($string [$k]);
85 }
86 }
87
88 if (!$full) {
89 $string = \array_slice($string, 0, 1);
90 }
91 return $string ? \implode(', ', $string) . ($diff->invert ? ' ago' : '') : 'just now';
92 }
93}
94
DateTime utilities Ubiquity\utils\base$UDateTime This class is part of Ubiquity.
Definition UDateTime.php:14
static mysqlDateTime(\DateTime $datetime)
Definition UDateTime.php:35
static longDatetime($datetime, $locale='en')
Definition UDateTime.php:54
static shortDate($date, $locale='en')
Definition UDateTime.php:44
static secondsToTime($seconds)
Definition UDateTime.php:24
static mysqlDate(\DateTime $date)
Definition UDateTime.php:31
static longDate($date, $locale='en')
Definition UDateTime.php:39
static elapsed($datetime, bool $full=false)
Definition UDateTime.php:67
static shortDatetime($datetime, $locale='en')
Definition UDateTime.php:49