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';
23 * convert() shorthand for UTC.
25 * @param string $time A date/time string
26 * @param string $format DateTime format string or Temporal constant
29 public static function utc($time, $format = self::MYSQL)
31 return self::convert($time, 'UTC', 'UTC', $format);
35 * convert() shorthand for local.
37 * @param string $time A date/time string
38 * @param string $format DateTime format string or Temporal constant
41 public static function local($time, $format = self::MYSQL)
43 return self::convert($time, date_default_timezone_get(), 'UTC', $format);
47 * convert() shorthand for timezoned now.
49 * @param string $format DateTime format string or Temporal constant
52 public static function timezoneNow($timezone, $format = self::MYSQL)
54 return self::convert('now', $timezone, 'UTC', $format);
58 * convert() shorthand for local now.
60 * @param string $format DateTime format string or Temporal constant
63 public static function localNow($format = self::MYSQL)
65 return self::local('now', $format);
69 * convert() shorthand for UTC now.
71 * @param string $format DateTime format string or Temporal constant
74 public static function utcNow($format = self::MYSQL)
76 return self::utc('now', $format);
80 * @brief General purpose date parse/convert/format function.
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
88 * @return string Formatted date according to given format
90 public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL)
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 === '') {
102 if (($s === '') || (!is_string($s))) {
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.
112 if (substr($s, 0, 10) <= '0001-01-01') {
113 if ($s < '0000-00-00') {
116 $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
117 return str_replace('1', '0', $d->format($format));
121 $from_obj = new DateTimeZone($tz_from);
122 } catch (Exception $e) {
123 $from_obj = new DateTimeZone('UTC');
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);
134 $to_obj = new DateTimeZone($tz_to);
135 } catch (Exception $e) {
136 $to_obj = new DateTimeZone('UTC');
139 $d->setTimeZone($to_obj);
141 return $d->format($format);