3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
24 use Friendica\Core\Logger;
34 const ATOM = 'Y-m-d\TH:i:s\Z';
35 const MYSQL = 'Y-m-d H:i:s';
36 const HTTP = 'D, d M Y H:i:s \G\M\T';
39 * convert() shorthand for UTC.
41 * @param string $time A date/time string
42 * @param string $format DateTime format string or Temporal constant
46 public static function utc($time, $format = self::MYSQL)
48 return self::convert($time, 'UTC', 'UTC', $format);
52 * convert() shorthand for local.
54 * @param string $time A date/time string
55 * @param string $format DateTime format string or Temporal constant
59 public static function local($time, $format = self::MYSQL)
61 return self::convert($time, date_default_timezone_get(), 'UTC', $format);
65 * convert() shorthand for timezoned now.
68 * @param string $format DateTime format string or Temporal constant
72 public static function timezoneNow($timezone, $format = self::MYSQL)
74 return self::convert('now', $timezone, 'UTC', $format);
78 * convert() shorthand for local now.
80 * @param string $format DateTime format string or Temporal constant
84 public static function localNow($format = self::MYSQL)
86 return self::local('now', $format);
90 * convert() shorthand for UTC now.
92 * @param string $format DateTime format string or Temporal constant
96 public static function utcNow($format = self::MYSQL)
98 return self::utc('now', $format);
102 * General purpose date parse/convert/format function.
104 * @param string $s Some parseable date/time string
105 * @param string $tz_to Destination timezone
106 * @param string $tz_from Source timezone
107 * @param string $format Output format recognised from php's DateTime class
108 * http://www.php.net/manual/en/datetime.format.php
110 * @return string Formatted date according to given format
113 public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL)
115 // Defaults to UTC if nothing is set, but throws an exception if set to empty string.
116 // Provide some sane defaults regardless.
117 if ($tz_from === '') {
125 if (($s === '') || (!is_string($s))) {
130 * Slight hackish adjustment so that 'zero' datetime actually returns what is intended
131 * otherwise we end up with -0001-11-30 ...
132 * add 32 days so that we at least get year 00, and then hack around the fact that
133 * months and days always start with 1.
135 if (substr($s, 0, 10) <= '0001-01-01') {
136 if ($s < '0000-00-00') {
139 $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
140 return str_replace('1', '0', $d->format($format));
144 $from_obj = new DateTimeZone($tz_from);
145 } catch (Exception $e) {
146 $from_obj = new DateTimeZone('UTC');
150 $d = new DateTime($s, $from_obj);
151 } catch (Exception $e) {
152 Logger::log('DateTimeFormat::convert: exception: ' . $e->getMessage());
153 $d = new DateTime('now', $from_obj);
157 $to_obj = new DateTimeZone($tz_to);
158 } catch (Exception $e) {
159 $to_obj = new DateTimeZone('UTC');
162 $d->setTimeZone($to_obj);
164 return $d->format($format);
168 * Checks, if the given string is a date with the pattern YYYY-MM
170 * @param string $dateString The given date
172 * @return boolean True, if the date is a valid pattern
174 public function isYearMonth(string $dateString)
176 // Check format (2019-01, 2019-1, 2019-10)
177 if (!preg_match('/^([12]\d{3}-(1[0-2]|0[1-9]|\d))$/', $dateString)) {
181 $date = DateTime::createFromFormat('Y-m', $dateString);
188 $now = new DateTime();
189 } catch (\Throwable $t) {
201 * Checks, if the given string is a date with the pattern YYYY-MM-DD
203 * @param string $dateString The given date
205 * @return boolean True, if the date is a valid pattern
207 public function isYearMonthDay(string $dateString)
209 $date = DateTime::createFromFormat('Y-m-d', $dateString);
214 if (DateTime::getLastErrors()['error_count'] || DateTime::getLastErrors()['warning_count']) {