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