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
30 public static function utc($time, $format = self::MYSQL)
32 return self::convert($time, 'UTC', 'UTC', $format);
36 * convert() shorthand for local.
38 * @param string $time A date/time string
39 * @param string $format DateTime format string or Temporal constant
42 public static function local($time, $format = self::MYSQL)
44 return self::convert($time, date_default_timezone_get(), 'UTC', $format);
48 * convert() shorthand for timezoned now.
50 * @param string $format DateTime format string or Temporal constant
53 public static function timezoneNow($timezone, $format = self::MYSQL)
55 return self::convert('now', $timezone, 'UTC', $format);
59 * convert() shorthand for local now.
61 * @param string $format DateTime format string or Temporal constant
64 public static function localNow($format = self::MYSQL)
66 return self::local('now', $format);
70 * convert() shorthand for UTC now.
72 * @param string $format DateTime format string or Temporal constant
75 public static function utcNow($format = self::MYSQL)
77 return self::utc('now', $format);
81 * @brief General purpose date parse/convert/format function.
83 * @param string $s Some parseable date/time string
84 * @param string $tz_to Destination timezone
85 * @param string $tz_from Source timezone
86 * @param string $format Output format recognised from php's DateTime class
87 * http://www.php.net/manual/en/datetime.format.php
89 * @return string Formatted date according to given format
91 public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL)
93 // Defaults to UTC if nothing is set, but throws an exception if set to empty string.
94 // Provide some sane defaults regardless.
95 if ($tz_from === '') {
103 if (($s === '') || (!is_string($s))) {
108 * Slight hackish adjustment so that 'zero' datetime actually returns what is intended
109 * otherwise we end up with -0001-11-30 ...
110 * add 32 days so that we at least get year 00, and then hack around the fact that
111 * months and days always start with 1.
113 if (substr($s, 0, 10) <= '0001-01-01') {
114 if ($s < '0000-00-00') {
117 $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
118 return str_replace('1', '0', $d->format($format));
122 $from_obj = new DateTimeZone($tz_from);
123 } catch (Exception $e) {
124 $from_obj = new DateTimeZone('UTC');
128 $d = new DateTime($s, $from_obj);
129 } catch (Exception $e) {
130 Logger::log('DateTimeFormat::convert: exception: ' . $e->getMessage());
131 $d = new DateTime('now', $from_obj);
135 $to_obj = new DateTimeZone($tz_to);
136 } catch (Exception $e) {
137 $to_obj = new DateTimeZone('UTC');
140 $d->setTimeZone($to_obj);
142 return $d->format($format);