/** * Transforms a date string in the configured timezone into a DateTime object. * * @param string $value A value as produced by PHP's date() function * * @return \DateTime An instance of \DateTime * * @throws TransformationFailedException If the given value is not a string, * if the date could not be parsed or * if the input timezone is not supported. */ public function reverseTransform($value) { if (empty($value)) { return; } if (!is_string($value)) { throw new TransformationFailedException('Expected a string.'); } $outputTz = new \DateTimeZone($this->outputTimezone); $dateTime = Date::createFromFormat($this->parseFormat, $value, $outputTz); $lastErrors = Date::getLastErrors(); if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) { throw new TransformationFailedException(implode(', ', array_merge(array_values($lastErrors['warnings']), array_values($lastErrors['errors'])))); } try { // On PHP versions < 5.3.7 we need to emulate the pipe operator // and reset parts not given in the format to their equivalent // of the UNIX base timestamp. if (!$this->parseUsingPipe) { list($year, $month, $day, $hour, $minute, $second) = explode('-', $dateTime->format('Y-m-d-H-i-s')); // Check which of the date parts are present in the pattern preg_match('/(' . '(?P<day>[djDl])|' . '(?P<month>[FMmn])|' . '(?P<year>[Yy])|' . '(?P<hour>[ghGH])|' . '(?P<minute>i)|' . '(?P<second>s)|' . '(?P<dayofyear>z)|' . '(?P<timestamp>U)|' . '[^djDlFMmnYyghGHiszU]' . ')*/', $this->parseFormat, $matches); // preg_match() does not guarantee to set all indices, so // set them unless given $matches = array_merge(array('day' => false, 'month' => false, 'year' => false, 'hour' => false, 'minute' => false, 'second' => false, 'dayofyear' => false, 'timestamp' => false), $matches); // Reset all parts that don't exist in the format to the // corresponding part of the UNIX base timestamp if (!$matches['timestamp']) { if (!$matches['dayofyear']) { if (!$matches['day']) { $day = 1; } if (!$matches['month']) { $month = 1; } } if (!$matches['year']) { $year = 1970; } if (!$matches['hour']) { $hour = 0; } if (!$matches['minute']) { $minute = 0; } if (!$matches['second']) { $second = 0; } $dateTime = $dateTime->setDate($year, $month, $day); $dateTime = $dateTime->setTime($hour, $minute, $second); } } if ($this->inputTimezone !== $this->outputTimezone) { $dateTime = $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone)); } } catch (\Exception $e) { throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); } return $dateTime; }