Esempio n. 1
0
 /**
  * @param integer|string|DateTime $value the value to be formatted. The following
  * types of value are supported:
  *
  * - an integer representing a UNIX timestamp
  * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php).
  *   The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.
  * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object
  *
  * @param string $format the format used to convert the value into a date string.
  * @param string $type 'date', 'time', or 'datetime'.
  * @throws InvalidConfigException if the date format is invalid.
  * @return string the formatted result.
  */
 private function formatDateTimeValue($value, $format, $type)
 {
     $timeZone = $this->timeZone;
     // avoid time zone conversion for date-only values
     if ($type === 'date') {
         list($timestamp, $hasTimeInfo) = $this->normalizeDatetimeValue($value, true);
         if (!$hasTimeInfo) {
             $timeZone = $this->defaultTimeZone;
         }
     } else {
         $timestamp = $this->normalizeDatetimeValue($value);
     }
     if ($timestamp === null) {
         return $this->nullDisplay;
     }
     // intl does not work with dates >=2038 or <=1901 on 32bit machines, fall back to PHP
     $year = $timestamp->format('Y');
     if ($this->_intlLoaded && !(PHP_INT_SIZE == 4 && ($year <= 1901 || $year >= 2038))) {
         if (strncmp($format, 'php:', 4) === 0) {
             $format = FormatConverter::convertDatePhpToIcu(substr($format, 4));
         }
         if (isset($this->_dateFormats[$format])) {
             if ($type === 'date') {
                 $formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], IntlDateFormatter::NONE, $timeZone);
             } elseif ($type === 'time') {
                 $formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, $this->_dateFormats[$format], $timeZone);
             } else {
                 $formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], $this->_dateFormats[$format], $timeZone);
             }
         } else {
             $formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $timeZone, null, $format);
         }
         if ($formatter === null) {
             throw new InvalidConfigException(intl_get_error_message());
         }
         // make IntlDateFormatter work with DateTimeImmutable
         if ($timestamp instanceof \DateTimeImmutable) {
             $timestamp = new DateTime($timestamp->format(DateTime::ISO8601), $timestamp->getTimezone());
         }
         return $formatter->format($timestamp);
     } else {
         if (strncmp($format, 'php:', 4) === 0) {
             $format = substr($format, 4);
         } else {
             $format = FormatConverter::convertDateIcuToPhp($format, $type, $this->locale);
         }
         if ($timeZone != null) {
             if ($timestamp instanceof \DateTimeImmutable) {
                 $timestamp = $timestamp->setTimezone(new DateTimeZone($timeZone));
             } else {
                 $timestamp->setTimezone(new DateTimeZone($timeZone));
             }
         }
         return $timestamp->format($format);
     }
 }
Esempio n. 2
0
 /**
  * Formats a timestamp using the specified format
  * @param integer $timestamp
  * @param string $format
  * @return string
  */
 private function formatTimestamp($timestamp, $format)
 {
     if (strncmp($format, 'php:', 4) === 0) {
         $format = substr($format, 4);
     } else {
         $format = FormatConverter::convertDateIcuToPhp($format, 'date');
     }
     $date = new DateTime();
     $date->setTimestamp($timestamp);
     $date->setTimezone(new \DateTimeZone($this->timestampAttributeTimeZone));
     return $date->format($format);
 }