/**
  * Format the date/time value (timestamp) as a string.
  *
  * @param int|\DateTime $timestamp The timestamp to format. \DateTime objects
  *                                 are supported as of PHP 5.3.4.
  *
  * @return string|bool The formatted value or false if formatting failed.
  *
  * @see http://www.php.net/manual/en/intldateformatter.format.php
  *
  * @throws MethodArgumentValueNotImplementedException 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 = PHP_VERSION_ID >= 50304 ? '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 (PHP_VERSION_ID < 50304 && !is_int($timestamp)) {
         $argumentError = 'datefmt_format: takes either an array  or an integer timestamp value ';
     } elseif (PHP_VERSION_ID >= 50304 && !is_int($timestamp) && !$timestamp instanceof \DateTime) {
         $argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object';
         if (PHP_VERSION_ID >= 50500 && !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) {
         IntlGlobals::setError(IntlGlobals::U_ILLEGAL_ARGUMENT_ERROR, $argumentError);
         $this->errorCode = IntlGlobals::getErrorCode();
         $this->errorMessage = IntlGlobals::getErrorMessage();
         return false;
     }
     // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
     if (PHP_VERSION_ID >= 50304 && $timestamp instanceof \DateTime) {
         $timestamp = $timestamp->getTimestamp();
     }
     $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
     $formatted = $transformer->format($this->createDateTime($timestamp));
     // behave like the intl extension
     IntlGlobals::setError(IntlGlobals::U_ZERO_ERROR);
     $this->errorCode = IntlGlobals::getErrorCode();
     $this->errorMessage = IntlGlobals::getErrorMessage();
     return $formatted;
 }
示例#2
0
 /**
  * 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 bool|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) {
         IntlGlobals::setError(IntlGlobals::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();
 }
示例#3
0
 /**
  * Set the error to the default U_ZERO_ERROR.
  */
 protected function resetError()
 {
     IntlGlobals::setError(IntlGlobals::U_ZERO_ERROR);
     $this->errorCode = IntlGlobals::getErrorCode();
     $this->errorMessage = IntlGlobals::getErrorMessage();
 }