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