Web Interfaces of PROSUME

Rfc3339.php 886B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace JsonSchema;
  3. class Rfc3339
  4. {
  5. const REGEX = '/^(\d{4}-\d{2}-\d{2}[T ]{1}\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/';
  6. /**
  7. * Try creating a DateTime instance
  8. *
  9. * @param string $string
  10. *
  11. * @return \DateTime|null
  12. */
  13. public static function createFromString($string)
  14. {
  15. if (!preg_match(self::REGEX, strtoupper($string), $matches)) {
  16. return null;
  17. }
  18. $dateAndTime = $matches[1];
  19. $microseconds = $matches[2] ?: '.000000';
  20. $timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00';
  21. $dateFormat = strpos($dateAndTime, 'T') === false ? 'Y-m-d H:i:s.uP' : 'Y-m-d\TH:i:s.uP';
  22. $dateTime = \DateTime::createFromFormat($dateFormat, $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC'));
  23. return $dateTime ?: null;
  24. }
  25. }