]> git.mxchange.org Git - friendica.git/blob - src/Util/DateTimeFormat.php
Remove unused dependency
[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  * 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          * 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
152         /**
153          * Checks, if the given string is a date with the pattern YYYY-MM
154          *
155          * @param string $dateString The given date
156          *
157          * @return boolean True, if the date is a valid pattern
158          */
159         public function isYearMonth(string $dateString)
160         {
161                 // Check format (2019-01, 2019-1, 2019-10)
162                 if (!preg_match('/^([12]\d{3}-(1[0-2]|0[1-9]|\d))$/', $dateString)) {
163                         return false;
164                 }
165
166                 $date = DateTime::createFromFormat('Y-m', $dateString);
167
168                 if (!$date) {
169                         return false;
170                 }
171
172                 try {
173                         $now = new DateTime();
174                 } catch (\Throwable $t) {
175                         return false;
176                 }
177
178                 if ($date > $now) {
179                         return false;
180                 }
181
182                 return true;
183         }
184
185         /**
186          * Checks, if the given string is a date with the pattern YYYY-MM-DD
187          *
188          * @param string $dateString The given date
189          *
190          * @return boolean True, if the date is a valid pattern
191          */
192         public function isYearMonthDay(string $dateString)
193         {
194                 $date = DateTime::createFromFormat('Y-m-d', $dateString);
195                 if (!$date) {
196                         return false;
197                 }
198
199                 if (DateTime::getLastErrors()['error_count'] || DateTime::getLastErrors()['warning_count']) {
200                         return false;
201                 }
202
203                 return true;
204         }
205 }