4 * @file src/Util/DateTimeFormat.php
7 namespace Friendica\Util;
9 use Friendica\Core\Logger;
15 * @brief Temporal class
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';
24 * convert() shorthand for UTC.
26 * @param string $time A date/time string
27 * @param string $format DateTime format string or Temporal constant
31 public static function utc($time, $format = self::MYSQL)
33 return self::convert($time, 'UTC', 'UTC', $format);
37 * convert() shorthand for local.
39 * @param string $time A date/time string
40 * @param string $format DateTime format string or Temporal constant
44 public static function local($time, $format = self::MYSQL)
46 return self::convert($time, date_default_timezone_get(), 'UTC', $format);
50 * convert() shorthand for timezoned now.
53 * @param string $format DateTime format string or Temporal constant
57 public static function timezoneNow($timezone, $format = self::MYSQL)
59 return self::convert('now', $timezone, 'UTC', $format);
63 * convert() shorthand for local now.
65 * @param string $format DateTime format string or Temporal constant
69 public static function localNow($format = self::MYSQL)
71 return self::local('now', $format);
75 * convert() shorthand for UTC now.
77 * @param string $format DateTime format string or Temporal constant
81 public static function utcNow($format = self::MYSQL)
83 return self::utc('now', $format);
87 * @brief General purpose date parse/convert/format function.
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
95 * @return string Formatted date according to given format
98 public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL)
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 === '') {
110 if (($s === '') || (!is_string($s))) {
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.
120 if (substr($s, 0, 10) <= '0001-01-01') {
121 if ($s < '0000-00-00') {
124 $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
125 return str_replace('1', '0', $d->format($format));
129 $from_obj = new DateTimeZone($tz_from);
130 } catch (Exception $e) {
131 $from_obj = new DateTimeZone('UTC');
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);
142 $to_obj = new DateTimeZone($tz_to);
143 } catch (Exception $e) {
144 $to_obj = new DateTimeZone('UTC');
147 $d->setTimeZone($to_obj);
149 return $d->format($format);
153 * Checks, if the given string is a date with the pattern YYYY-MM
155 * @param string $dateString The given date
157 * @return boolean True, if the date is a valid pattern
159 public function isYearMonth(string $dateString)
161 // Check format (2019-01, 2019-1, 2019-10)
162 if (!preg_match('/^([12]\d{3}-(1[0-2]|0[1-9]|\d))$/', $dateString)) {
166 $date = DateTime::createFromFormat('Y-m', $dateString);
173 $now = new DateTime();
174 } catch (\Throwable $t) {