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