Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
4.7 KiB

  1. <?php
  2. /**
  3. * @file src/Util/DateTimeFormat.php
  4. */
  5. namespace Friendica\Util;
  6. use Friendica\Core\Logger;
  7. use DateTime;
  8. use DateTimeZone;
  9. use Exception;
  10. /**
  11. * @brief Temporal class
  12. */
  13. class DateTimeFormat
  14. {
  15. const ATOM = 'Y-m-d\TH:i:s\Z';
  16. const MYSQL = 'Y-m-d H:i:s';
  17. const HTTP = 'D, d M Y H:i:s \G\M\T';
  18. /**
  19. * convert() shorthand for UTC.
  20. *
  21. * @param string $time A date/time string
  22. * @param string $format DateTime format string or Temporal constant
  23. * @return string
  24. * @throws Exception
  25. */
  26. public static function utc($time, $format = self::MYSQL)
  27. {
  28. return self::convert($time, 'UTC', 'UTC', $format);
  29. }
  30. /**
  31. * convert() shorthand for local.
  32. *
  33. * @param string $time A date/time string
  34. * @param string $format DateTime format string or Temporal constant
  35. * @return string
  36. * @throws Exception
  37. */
  38. public static function local($time, $format = self::MYSQL)
  39. {
  40. return self::convert($time, date_default_timezone_get(), 'UTC', $format);
  41. }
  42. /**
  43. * convert() shorthand for timezoned now.
  44. *
  45. * @param $timezone
  46. * @param string $format DateTime format string or Temporal constant
  47. * @return string
  48. * @throws Exception
  49. */
  50. public static function timezoneNow($timezone, $format = self::MYSQL)
  51. {
  52. return self::convert('now', $timezone, 'UTC', $format);
  53. }
  54. /**
  55. * convert() shorthand for local now.
  56. *
  57. * @param string $format DateTime format string or Temporal constant
  58. * @return string
  59. * @throws Exception
  60. */
  61. public static function localNow($format = self::MYSQL)
  62. {
  63. return self::local('now', $format);
  64. }
  65. /**
  66. * convert() shorthand for UTC now.
  67. *
  68. * @param string $format DateTime format string or Temporal constant
  69. * @return string
  70. * @throws Exception
  71. */
  72. public static function utcNow($format = self::MYSQL)
  73. {
  74. return self::utc('now', $format);
  75. }
  76. /**
  77. * @brief General purpose date parse/convert/format function.
  78. *
  79. * @param string $s Some parseable date/time string
  80. * @param string $tz_to Destination timezone
  81. * @param string $tz_from Source timezone
  82. * @param string $format Output format recognised from php's DateTime class
  83. * http://www.php.net/manual/en/datetime.format.php
  84. *
  85. * @return string Formatted date according to given format
  86. * @throws Exception
  87. */
  88. public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL)
  89. {
  90. // Defaults to UTC if nothing is set, but throws an exception if set to empty string.
  91. // Provide some sane defaults regardless.
  92. if ($tz_from === '') {
  93. $tz_from = 'UTC';
  94. }
  95. if ($tz_to === '') {
  96. $tz_to = 'UTC';
  97. }
  98. if (($s === '') || (!is_string($s))) {
  99. $s = 'now';
  100. }
  101. /*
  102. * Slight hackish adjustment so that 'zero' datetime actually returns what is intended
  103. * otherwise we end up with -0001-11-30 ...
  104. * add 32 days so that we at least get year 00, and then hack around the fact that
  105. * months and days always start with 1.
  106. */
  107. if (substr($s, 0, 10) <= '0001-01-01') {
  108. if ($s < '0000-00-00') {
  109. $s = '0000-00-00';
  110. }
  111. $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
  112. return str_replace('1', '0', $d->format($format));
  113. }
  114. try {
  115. $from_obj = new DateTimeZone($tz_from);
  116. } catch (Exception $e) {
  117. $from_obj = new DateTimeZone('UTC');
  118. }
  119. try {
  120. $d = new DateTime($s, $from_obj);
  121. } catch (Exception $e) {
  122. Logger::log('DateTimeFormat::convert: exception: ' . $e->getMessage());
  123. $d = new DateTime('now', $from_obj);
  124. }
  125. try {
  126. $to_obj = new DateTimeZone($tz_to);
  127. } catch (Exception $e) {
  128. $to_obj = new DateTimeZone('UTC');
  129. }
  130. $d->setTimeZone($to_obj);
  131. return $d->format($format);
  132. }
  133. /**
  134. * Checks, if the given string is a date with the pattern YYYY-MM
  135. *
  136. * @param string $dateString The given date
  137. *
  138. * @return boolean True, if the date is a valid pattern
  139. */
  140. public function isYearMonth(string $dateString)
  141. {
  142. // Check format (2019-01, 2019-1, 2019-10)
  143. if (!preg_match('/^([12]\d{3}-(1[0-2]|0[1-9]|\d))$/', $dateString)) {
  144. return false;
  145. }
  146. $date = DateTime::createFromFormat('Y-m', $dateString);
  147. if (!$date) {
  148. return false;
  149. }
  150. try {
  151. $now = new DateTime();
  152. } catch (\Throwable $t) {
  153. return false;
  154. }
  155. if ($date > $now) {
  156. return false;
  157. }
  158. return true;
  159. }
  160. /**
  161. * Checks, if the given string is a date with the pattern YYYY-MM-DD
  162. *
  163. * @param string $dateString The given date
  164. *
  165. * @return boolean True, if the date is a valid pattern
  166. */
  167. public function isYearMonthDay(string $dateString)
  168. {
  169. $date = DateTime::createFromFormat('Y-m-d', $dateString);
  170. if (!$date) {
  171. return false;
  172. }
  173. if (DateTime::getLastErrors()['error_count'] || DateTime::getLastErrors()['warning_count']) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. }