]> git.mxchange.org Git - friendica.git/blob - src/Util/DateTimeFormat.php
Merge pull request #6447 from annando/peertube
[friendica.git] / src / Util / DateTimeFormat.php
1 <?php
2
3 /**
4  * @file src/Util/DateTimeFormat.php
5  */
6
7 namespace Friendica\Util;
8
9 use Friendica\Core\Logger;
10 use DateTime;
11 use DateTimeZone;
12 use Exception;
13
14 /**
15  * @brief Temporal class
16  */
17 class DateTimeFormat
18 {
19         const ATOM = 'Y-m-d\TH:i:s\Z';
20         const MYSQL = 'Y-m-d H:i:s';
21         const HTTP = 'D, d M Y H:i:s \G\M\T';
22
23         /**
24          * convert() shorthand for UTC.
25          *
26          * @param string $time   A date/time string
27          * @param string $format DateTime format string or Temporal constant
28          * @return string
29          */
30         public static function utc($time, $format = self::MYSQL)
31         {
32                 return self::convert($time, 'UTC', 'UTC', $format);
33         }
34
35         /**
36          * convert() shorthand for local.
37          *
38          * @param string $time   A date/time string
39          * @param string $format DateTime format string or Temporal constant
40          * @return string
41          */
42         public static function local($time, $format = self::MYSQL)
43         {
44                 return self::convert($time, date_default_timezone_get(), 'UTC', $format);
45         }
46
47         /**
48          * convert() shorthand for timezoned now.
49          *
50          * @param string $format DateTime format string or Temporal constant
51          * @return string
52          */
53         public static function timezoneNow($timezone, $format = self::MYSQL)
54         {
55                 return self::convert('now', $timezone, 'UTC', $format);
56         }
57
58         /**
59          * convert() shorthand for local now.
60          *
61          * @param string $format DateTime format string or Temporal constant
62          * @return string
63          */
64         public static function localNow($format = self::MYSQL)
65         {
66                 return self::local('now', $format);
67         }
68
69         /**
70          * convert() shorthand for UTC now.
71          *
72          * @param string $format DateTime format string or Temporal constant
73          * @return string
74          */
75         public static function utcNow($format = self::MYSQL)
76         {
77                 return self::utc('now', $format);
78         }
79
80         /**
81          * @brief General purpose date parse/convert/format function.
82          *
83          * @param string $s       Some parseable date/time string
84          * @param string $tz_to   Destination timezone
85          * @param string $tz_from Source timezone
86          * @param string $format  Output format recognised from php's DateTime class
87          *   http://www.php.net/manual/en/datetime.format.php
88          *
89          * @return string Formatted date according to given format
90          */
91         public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL)
92         {
93                 // Defaults to UTC if nothing is set, but throws an exception if set to empty string.
94                 // Provide some sane defaults regardless.
95                 if ($tz_from === '') {
96                         $tz_from = 'UTC';
97                 }
98
99                 if ($tz_to === '') {
100                         $tz_to = 'UTC';
101                 }
102
103                 if (($s === '') || (!is_string($s))) {
104                         $s = 'now';
105                 }
106
107                 /*
108                  * Slight hackish adjustment so that 'zero' datetime actually returns what is intended
109                  * otherwise we end up with -0001-11-30 ...
110                  * add 32 days so that we at least get year 00, and then hack around the fact that
111                  * months and days always start with 1.
112                  */
113                 if (substr($s, 0, 10) <= '0001-01-01') {
114                         if ($s < '0000-00-00') {
115                                 $s = '0000-00-00';
116                         }
117                         $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
118                         return str_replace('1', '0', $d->format($format));
119                 }
120
121                 try {
122                         $from_obj = new DateTimeZone($tz_from);
123                 } catch (Exception $e) {
124                         $from_obj = new DateTimeZone('UTC');
125                 }
126
127                 try {
128                         $d = new DateTime($s, $from_obj);
129                 } catch (Exception $e) {
130                         Logger::log('DateTimeFormat::convert: exception: ' . $e->getMessage());
131                         $d = new DateTime('now', $from_obj);
132                 }
133
134                 try {
135                         $to_obj = new DateTimeZone($tz_to);
136                 } catch (Exception $e) {
137                         $to_obj = new DateTimeZone('UTC');
138                 }
139
140                 $d->setTimeZone($to_obj);
141
142                 return $d->format($format);
143         }
144 }