Neos\Flow\I18n\Formatter\DatetimeFormatter::format PHP Метод

format() публичный Метод

Formats provided value using optional style properties
public format ( mixed $value, Locale $locale, array $styleProperties = [] ) : string
$value mixed Formatter-specific variable to format (can be integer, \DateTime, etc)
$locale Neos\Flow\I18n\Locale Locale to use
$styleProperties array Integer-indexed array of formatter-specific style properties (can be empty)
Результат string String representation of $value provided, or (string)$value
    public function format($value, Locale $locale, array $styleProperties = [])
    {
        if (isset($styleProperties[0])) {
            $formatType = $styleProperties[0];
            DatesReader::validateFormatType($formatType);
        } else {
            $formatType = DatesReader::FORMAT_TYPE_DATETIME;
        }
        if (isset($styleProperties[1])) {
            $formatLength = $styleProperties[1];
            DatesReader::validateFormatLength($formatLength);
        } else {
            $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT;
        }
        switch ($formatType) {
            case DatesReader::FORMAT_TYPE_DATE:
                return $this->formatDate($value, $locale, $formatLength);
            case DatesReader::FORMAT_TYPE_TIME:
                return $this->formatTime($value, $locale, $formatLength);
            default:
                return $this->formatDateTime($value, $locale, $formatLength);
        }
    }

Usage Example

 /**
  * Render the supplied DateTime object as a formatted date.
  *
  * @param mixed $date either a \DateTime object or a string that is accepted by \DateTime constructor
  * @param string $format Format String which is taken to format the Date/Time if none of the locale options are set.
  * @param string $localeFormatType Whether to format (according to locale set in $forceLocale) date, time or datetime. Must be one of Neos\Flow\I18n\Cldr\Reader\DatesReader::FORMAT_TYPE_*'s constants.
  * @param string $localeFormatLength Format length if locale set in $forceLocale. Must be one of Neos\Flow\I18n\Cldr\Reader\DatesReader::FORMAT_LENGTH_*'s constants.
  * @param string $cldrFormat Format string in CLDR format (see http://cldr.unicode.org/translation/date-time)
  * @throws ViewHelperException
  * @return string Formatted date
  * @api
  */
 public function render($date = null, $format = 'Y-m-d', $localeFormatType = null, $localeFormatLength = null, $cldrFormat = null)
 {
     if ($date === null) {
         $date = $this->renderChildren();
         if ($date === null) {
             return '';
         }
     }
     if (!$date instanceof \DateTimeInterface) {
         try {
             $date = new \DateTime($date);
         } catch (\Exception $exception) {
             throw new ViewHelperException('"' . $date . '" could not be parsed by \\DateTime constructor.', 1241722579, $exception);
         }
     }
     $useLocale = $this->getLocale();
     if ($useLocale !== null) {
         try {
             if ($cldrFormat !== null) {
                 $output = $this->datetimeFormatter->formatDateTimeWithCustomPattern($date, $cldrFormat, $useLocale);
             } else {
                 $output = $this->datetimeFormatter->format($date, $useLocale, array($localeFormatType, $localeFormatLength));
             }
         } catch (I18nException $exception) {
             throw new ViewHelperException(sprintf('An error occurred while trying to format the given date/time: "%s"', $exception->getMessage()), 1342610987, $exception);
         }
     } else {
         $output = $date->format($format);
     }
     return $output;
 }