/** * Calculates the Unix timestamp based on the matched values by the reverse matching regular * expression of parse() * * @param DateTime $dateTime The DateTime object to be used to calculate the timestamp * @param array $options An array with the matched values to be used to calculate the timestamp * * @return Boolean|int The calculated timestamp or false if matched date is invalid */ protected function calculateUnixTimestamp(\DateTime $dateTime, array $options) { $options = $this->getDefaultValueForOptions($options); $year = $options['year']; $month = $options['month']; $day = $options['day']; $hour = $options['hour']; $hourInstance = $options['hourInstance']; $minute = $options['minute']; $second = $options['second']; $marker = $options['marker']; $timezone = $options['timezone']; // If month is false, return immediately (intl behavior) if (false === $month) { StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Date parsing failed'); return false; } // Normalize hour if ($hourInstance instanceof HourTransformer) { $hour = $hourInstance->normalizeHour($hour, $marker); } // Set the timezone if different from the default one if (null !== $timezone && $timezone !== $this->timezone) { $dateTime->setTimezone(new \DateTimeZone($timezone)); } // Normalize yy year preg_match_all($this->regExp, $this->pattern, $matches); if (in_array('yy', $matches[0])) { $dateTime->setTimestamp(time()); $year = $year > $dateTime->format('y') + 20 ? 1900 + $year : 2000 + $year; } $dateTime->setDate($year, $month, $day); $dateTime->setTime($hour, $minute, $second); return $dateTime->getTimestamp(); }
/** * Set the error to the default U_ZERO_ERROR */ protected function resetError() { StubIntl::setError(StubIntl::U_ZERO_ERROR); $this->errorCode = StubIntl::getErrorCode(); $this->errorMessage = StubIntl::getErrorMessage(); }
/** * Format the date/time value (timestamp) as a string * * @param mixed $timestamp Unix timestamp to format * * @return string The formatted value * * @see http://www.php.net/manual/en/intldateformatter.format.php * * @throws NotImplementedException If one of the formatting characters is not implemented */ public function format($timestamp) { // intl allows timestamps to be passed as arrays - we don't if (is_array($timestamp)) { $message = version_compare(\PHP_VERSION, '5.3.4', '>=') ? 'Only integer unix timestamps and DateTime objects are supported' : 'Only integer unix timestamps are supported'; throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, $message); } // behave like the intl extension $argumentError = null; if (version_compare(\PHP_VERSION, '5.3.4', '<') && !is_int($timestamp)) { $argumentError = 'datefmt_format: takes either an array or an integer timestamp value '; } elseif (version_compare(\PHP_VERSION, '5.3.4', '>=') && !is_int($timestamp) && !$timestamp instanceof \DateTime) { $argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object'; if (version_compare(\PHP_VERSION, '5.5.0-dev', '>=') && !is_int($timestamp)) { $argumentError = sprintf('datefmt_format: string \'%s\' is not numeric, which would be required for it to be a valid date', $timestamp); } } if (null !== $argumentError) { StubIntl::setError(StubIntl::U_ILLEGAL_ARGUMENT_ERROR, $argumentError); $this->errorCode = StubIntl::getErrorCode(); $this->errorMessage = StubIntl::getErrorMessage(); return false; } // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances if (version_compare(\PHP_VERSION, '5.3.4', '>=') && $timestamp instanceof \DateTime) { $timestamp = $timestamp->getTimestamp(); } $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); $formatted = $transformer->format($this->createDateTime($timestamp)); // behave like the intl extension StubIntl::setError(StubIntl::U_ZERO_ERROR); $this->errorCode = StubIntl::getErrorCode(); $this->errorMessage = StubIntl::getErrorMessage(); return $formatted; }