public function buildForm(FormBuilderInterface $builder, array $options) { $format = $options['format']; $pattern = null; $allowedFormatOptionValues = array(\IntlDateFormatter::FULL, \IntlDateFormatter::LONG, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); // If $format is not in the allowed options, it's considered as the pattern of the formatter if it is a string if (!in_array($format, $allowedFormatOptionValues, true)) { if (is_string($format)) { $format = self::DEFAULT_FORMAT; $pattern = $options['format']; } else { throw new CreationException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom pattern'); } } $formatter = new \IntlDateFormatter(\Locale::getDefault(), $format, \IntlDateFormatter::NONE, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern); $formatter->setLenient(false); $builder->addViewTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $format, \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $pattern)); if ('string' === $options['input']) { $builder->addModelTransformer(new ReversedTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d'))); } elseif ('timestamp' === $options['input']) { $builder->addModelTransformer(new ReversedTransformer(new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']))); } elseif ('array' === $options['input']) { $builder->addModelTransformer(new ReversedTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day')))); } $builder->setAttribute('date_pattern', $formatter->getPattern()); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $format = function (Options $options) { $date_format = \IntlDateFormatter::NONE; $time_format = \IntlDateFormatter::NONE; if ($options['date_picker']) { $date_format = \IntlDateFormatter::SHORT; } if ($options['time_picker']) { $time_format = $options['with_seconds'] ? \IntlDateFormatter::MEDIUM : \IntlDateFormatter::SHORT; } $formater = new \IntlDateFormatter($options['locale'], $date_format, $time_format, $options['user_timezone'], \IntlDateFormatter::GREGORIAN, null); $formater->setLenient(false); $pattern = $formater->getPattern(); if (false === strpos($pattern, 'yyyy')) { if (false !== strpos($pattern, 'yy')) { $pattern = str_replace('yy', 'yyyy', $pattern); } elseif (false !== strpos($pattern, 'y')) { $pattern = str_replace('y', 'yyyy', $pattern); } } return $pattern; }; $resolver->setDefaults(array('widget' => 'single_text', 'locale' => \Locale::getDefault(), 'user_timezone' => null, 'date_picker' => true, 'time_picker' => true, 'time_picker_first' => false, 'button_id' => null, 'open_focus' => true, 'hour_min' => null, 'hour_max' => null, 'hour_step' => null, 'minute_min' => null, 'minute_max' => null, 'minute_step' => null, 'second_min' => null, 'second_max' => null, 'second_step' => null, 'format' => $format, 'empty_value' => null, 'with_minutes' => true, 'with_seconds' => false)); }
function d(IntlDateFormatter $df) { global $ts; echo $df->format($ts), "\n"; var_dump($df->getCalendar(), $df->getCalendarObject()->getType(), $df->getCalendarObject()->getTimeZone()->getId()); echo "\n"; }
public function createView(ViewFactory $factory, $data, array $options) : ViewInterface { $dateFormat = $this->resolveFormat($options['date_format']); $timeFormat = $this->resolveFormat($options['time_format']); $formatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, null, \IntlDateFormatter::GREGORIAN); return new DateTimeView($formatter->format($data), $data, $options['tag']); }
/** * Returns a string representation of the value. * * This method returns the equivalent PHP tokens for most scalar types * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped * in double quotes ("). Objects, arrays and resources are formatted as * "object", "array" and "resource". If the parameter $prettyDateTime * is set to true, {@link \DateTime} objects will be formatted as * RFC-3339 dates ("Y-m-d H:i:s"). * * Be careful when passing message parameters to a constraint violation * that (may) contain objects, arrays or resources. These parameters * should only be displayed for technical users. Non-technical users * won't know what an "object", "array" or "resource" is and will be * confused by the violation message. * * @param mixed $value The value to format as string * @param bool $prettyDateTime Whether to format {@link \DateTime} * objects as RFC-3339 dates ("Y-m-d H:i:s") * * @return string The string representation of the passed value */ protected function formatValue($value, $prettyDateTime = false) { if ($prettyDateTime && $value instanceof \DateTime) { if (class_exists('IntlDateFormatter')) { $locale = \Locale::getDefault(); $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); return $formatter->format($value); } return $value->format('Y-m-d H:i:s'); } if (is_object($value)) { return 'object'; } if (is_array($value)) { return 'array'; } if (is_string($value)) { return '"' . $value . '"'; } if (is_resource($value)) { return 'resource'; } if (null === $value) { return 'null'; } if (false === $value) { return 'false'; } if (true === $value) { return 'true'; } return (string) $value; }
public function query($departure, $arrival, \DateTime $date = null) { $date = $date ? $date : new \DateTime(); $fmt = new \IntlDateFormatter('en_EN', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, date_default_timezone_get(), \IntlDateFormatter::TRADITIONAL); $response = $this->client->post('bin/query.exe/fn', array('headers' => array('host' => 'fahrplan.sbb.ch', 'origin' => 'http://fahrplan.sbb.ch', 'referer' => 'http://fahrplan.sbb.ch/bin/query.exe/fn'), 'form_params' => array('queryPageDisplayed' => 'yes', 'HWAI=JS!ajax' => 'yes', 'HWAI=JS!js' => 'yes', 'HWAI' => '~CONNECTION!', 'REQ0Total_KissRideMotorClass' => '404', 'REQ0Total_KissRideCarClass' => '5', 'REQ0Total_KissRide_maxDist' => '10000000', 'REQ0Total_KissRide_minDist' => '0', 'REQComparisonCarload' => '0', 'REQ0JourneyStopsS0G' => $departure['value'], 'REQ0JourneyStopsS0ID' => $departure['id'], 'REQ0JourneyStopsS0A' => '255', 'REQ0JourneyStopsZ0G' => $arrival['value'], 'REQ0JourneyStopsZ0ID' => $arrival['id'], 'REQ0JourneyStopsZ0A' => '255', 'REQ0JourneyStops1.0G' => '', 'REQ0JourneyStops1.0A' => '1', 'REQ0JourneyStopover1' => '', 'date' => sprintf('%s, %s', substr($fmt->format($date), 0, 2), $date->format('d-m-y')), 'REQ0JourneyTime' => $date->format('H:i'), 'REQ0HafasSearchForw' => '1', 'REQ0JourneyStops2.0G' => '', 'REQ0JourneyStops2.0A' => '1', 'REQ0JourneyStopover2' => '', 'REQ0JourneyStops3.0G' => '', 'REQ0JourneyStops3.0A' => '1', 'REQ0JourneyStopover3' => '', 'REQ0JourneyStops4.0G' => '', 'REQ0JourneyStops4.0A' => '1', 'REQ0JourneyStopover4' => '', 'REQ0JourneyStops5.0G' => '', 'REQ0JourneyStops5.0A' => '1', 'REQ0JourneyStopover5' => '', 'existOptimizePrice' => '0', 'existUnsharpSearch' => 'yes', 'REQ0HafasChangeTime' => '0:1', 'existHafasAttrExc' => 'yes', 'REQ0JourneyProduct_prod_0' => '1', 'existProductBits0' => 'yes', 'REQ0JourneyProduct_prod_1' => '1', 'REQ0JourneyProduct_prod_2' => '1', 'REQ0JourneyProduct_prod_3' => '1', 'REQ0JourneyProduct_prod_4' => '1', 'REQ0JourneyProduct_prod_5' => '1', 'REQ0JourneyProduct_prod_6' => '1', 'REQ0JourneyProduct_prod_7' => '1', 'REQ0JourneyProduct_prod_8' => '1', 'REQ0JourneyProduct_prod_9' => '1', 'REQ0JourneyProduct_opt_section_0_list' => '0:0000', 'disableBaim' => 'yes', 'REQ0HafasHandicapLimit' => '4:4', 'changeQueryInputData' => 'yes', 'start' => 'Chercher correspondance'))); $crawler = new Crawler(utf8_encode((string) $response->getBody())); $overviews = array(); $date = ''; // Get overviews $crawler->filter('.hfs_overview .overview')->each(function (Crawler $node) use(&$overviews, &$date) { // Update date if (false !== strpos($node->getNode(0)->getAttribute('class'), 'dateHint')) { // Subtract 8 last chars $date = substr($text = trim($node->text()), ($len = strlen($text)) - 8, $len); return; } $overviews[] = array_filter(array($date, count($n = $node->filter('.time.departure')) ? substr(trim($n->text()), 0, 5) : null, count($n = $node->filter('.time.arrival')) ? substr(trim($n->text()), 0, 5) : null, count($n = $node->filter('.duration')) ? trim($n->text()) : null, count($n = $node->filter('.changes')) ? trim($n->text()) : null, count($n = $node->filter('.products')) ? trim($n->text()) : null, sprintf('%s%s', count($n = $node->filter('.top img')) ? trim($n->attr('alt')) : null, count($n = $node->filter('.him_icon img')) ? trim($n->attr('alt')) : null)), function ($v) { return $v !== null; }); }); // Combine overviews $_overviews = array(); for ($i = 0; $i < count($overviews) / 2; $i++) { $overview = $overviews[$i * 2] + $overviews[$i * 2 + 1]; ksort($overview); $_overviews[] = array_combine(array('date', 'departure', 'arrival', 'duration', 'change', 'product', 'infos'), $overview); } return $_overviews; }
public function format($datetime, $dateFormat = null, $timeFormat = null, $timezone = null, $locale = null) { $dt = null; $tz = null; if (null !== $timezone) { $tz = is_string($timezone) ? new \DateTimeZone($timezone) : $timezone; } if ($datetime instanceof \DateTimeImmutable) { $dt = new \DateTime($datetime->format('Y-m-d H:i:s'), $tz); } elseif (!$datetime instanceof \DateTime) { $dt = new \DateTime($datetime, $tz); } else { $dt = clone $datetime; } if (null === $tz) { $tz = $dt->getTimezone(); } if (null === $locale) { $locale = \Locale::getDefault(); } if (null === $dateFormat) { $dateFormat = \IntlDateFormatter::MEDIUM; } if (null === $timeFormat) { $timeFormat = \IntlDateFormatter::NONE; } $currentLocale = \Locale::getDefault(); \Locale::setDefault($locale); $formatter = new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $tz); $result = $formatter->format($dt); \Locale::setDefault($currentLocale); return $result; }
function d(IntlDateFormatter $df) { global $ts; echo $df->format($ts), "\n"; var_dump($df->getTimeZoneID(), $df->getTimeZone()->getID()); echo "\n"; }
/** * {@inheritdoc} */ public function finishView(FormView $view, FormInterface $form, array $options) { $format = $options['format']; if (isset($options['date_format']) && is_string($options['date_format'])) { $format = $options['date_format']; } elseif (is_int($format)) { $timeFormat = $options['dp_pick_time'] ? DateTimeType::DEFAULT_TIME_FORMAT : \IntlDateFormatter::NONE; $intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $format, $timeFormat, null, \IntlDateFormatter::GREGORIAN, null); $format = $intlDateFormatter->getPattern(); } // use seconds if it's allowe in format $options['dp_use_seconds'] = strpos($format, 's') !== false; $view->vars['moment_format'] = $this->formatConverter->convert($format); $view->vars['type'] = 'text'; $dpOptions = array(); foreach ($options as $key => $value) { if (false !== strpos($key, "dp_")) { // We remove 'dp_' and camelize the options names $dpKey = substr($key, 3); $dpKey = preg_replace_callback('/_([a-z])/', function ($c) { return strtoupper($c[1]); }, $dpKey); $dpOptions[$dpKey] = $value; } } $view->vars['datepicker_use_button'] = empty($options['datepicker_use_button']) ? false : true; $view->vars['dp_options'] = $dpOptions; }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { $value = $form->getViewData(); // set string representation if (true === $value) { $value = 'true'; } elseif (false === $value) { $value = 'false'; } elseif (null === $value) { $value = 'null'; } elseif (is_array($value)) { $value = implode(', ', $value); } elseif ($value instanceof \DateTime) { $dateFormat = is_int($options['date_format']) ? $options['date_format'] : DateType::DEFAULT_FORMAT; $timeFormat = is_int($options['time_format']) ? $options['time_format'] : DateType::DEFAULT_FORMAT; $calendar = \IntlDateFormatter::GREGORIAN; $pattern = is_string($options['date_pattern']) ? $options['date_pattern'] : null; $formatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, 'UTC', $calendar, $pattern); $formatter->setLenient(false); $value = $formatter->format($value); } elseif (is_object($value)) { if (method_exists($value, '__toString')) { $value = $value->__toString(); } else { $value = get_class($value); } } $view->vars['value'] = (string) $value; }
function openfit_base_preprocess_comment(&$variables) { $openfit_node_types = array('activity' => TRUE); if (!isset($variables['node']) || !isset($openfit_node_types[$variables['node']->type])) { return; } $comment = $variables['comment']; // Remove the standard comment links: reply, edit, delete unset($variables['content']['links']['comment']['#links']); // Add a delete menu if the user posted the comment or is admin global $user; if (user_access('administer comments') || $user->uid == $comment->uid) { $url = drupal_get_path_alias('node/' . $variables['node']->nid) . '/comments'; $variables['content']['links']['comment']['#links'] = array('comment-delete' => array('title' => ' ', 'href' => 'comment/' . $comment->cid . '/delete', 'query' => array('destination' => $url), 'html' => TRUE)); } // Display "XX ago" for posts less than 1 day, otherwise use locale to format datetime $ago = time() - $comment->created; if ($ago < 86400) { $variables['created'] = t('!interval ago', array('!interval' => format_interval(time() - $comment->created))); } else { $locale = OpenFitUserSetting::getCurrentUserLocale(); $fmt = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); $created = new DateTime('now'); $created->setTimestamp($comment->created); $variables['created'] = $fmt->format($created); } $variables['submitted'] = $variables['author'] . ' ' . '<time datetime="' . $variables['datetime'] . '" pubdate="pubdate">' . $variables['created'] . '</time>'; }
public function __invoke($value, $format = 'Y-m-d H:i:s', $dateType = \IntlDateFormatter::SHORT, $timeType = \IntlDateFormatter::NONE) { $translator = $this->getServiceLocator()->getServiceLocator()->get('translator'); $locale = $translator->getTranslator()->getLocale(); $formatter = new \IntlDateFormatter($locale, $dateType, $timeType, null, null, $format); $date = new \DateTime($value); return $formatter->format($date); }
/** * {@inheritdoc} */ public function getFormat($locale) { if (isset($this->dateFormats[$locale])) { return $this->dateFormats[$locale]; } $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE); $icuFormat = $formatter->getPattern(); return $this->converter->convert($icuFormat); }
/** * {@inheritdoc} */ public function format($value, array $options = []) { if (!$value instanceof \DateTimeInterface) { throw new InvalidTypeException(sprintf('The number formatter expects a numeric value, got "%s".', is_object($value) ? get_class($value) : gettype($value))); } $formatter = new \IntlDateFormatter($this->localeContext->getLocale(), isset($options['date_format']) ? $options['date_format'] : \IntlDateFormatter::MEDIUM, isset($options['time_format']) ? $options['time_format'] : \IntlDateFormatter::MEDIUM, isset($options['timezone']) ? $options['timezone'] : $value->getTimezone(), isset($options['calendar']) ? $options['calendar'] : null, isset($options['pattern']) ? $options['pattern'] : null); $formatter->setLenient(isset($options['lenient']) ? $options['lenient'] : false); return $formatter->format($value); }
private function formatDate(\DateTime $date) { if (null === $this->options['format']) { $locale = \Locale::getDefault(); $ftm = new \IntlDateFormatter($locale, \IntlDateFormatter::SHORT, \IntlDateFormatter::SHORT); return $ftm->format($date); } return $date->format($this->options['format']); }
/** * @param string|\DateTimeInterface $date * * @return string */ public function getDateFormatted($date) : string { if ($date instanceof \DateTimeInterface) { return $this->formatter->format($date->getTimestamp()); } elseif (is_string($date)) { return $this->formatter->format(strtotime($date)); } else { return ''; } }
/** * @return array */ protected function getMonthNames() { $formatter = new \IntlDateFormatter(Yii::$app->language, \IntlDateFormatter::FULL, \IntlDateFormatter::FULL); $formatter->setPattern('MMMM'); $list = []; for ($i = 0; $i < 12; $i++) { $list[] = $formatter->format(mktime(0, 0, 0, $i, 1, 1970)); } $list[] = array_shift($list); return $list; }
public function ruDateFormat($dateRaw) { if ($dateRaw instanceof \DateTime) { $dateTime = $dateRaw; } else { $dateTime = new \DateTime($dateRaw); } $formatter = new \IntlDateFormatter('ru_RU', \IntlDateFormatter::NONE, \IntlDateFormatter::NONE); $formatter->setPattern('d MMMM YYYY'); return $formatter->format($dateTime); }
public function buildForm(FormBuilderInterface $builder, array $options) { // Create the sample date and time data to be displayed // in the select form fields $now = new \DateTime(); // Set user's timezone //$now->setTimezone(new \DateTimeZone($options['data']->getTimezone())); // Format output according to user's locale $localeFormat = new \IntlDateFormatter($options['data']->getLocale(), \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, (new \DateTimeZone($options['data']->getTimezone()))->getName()); if (is_array($this->formats['date']) && count($this->formats['date']) && is_array($this->formats['time']) && count($this->formats['time'])) { foreach ($this->formats['date'] as $dateFormat) { // Display format with current date to user $localeFormat->setPattern($dateFormat); $dateFormats[$dateFormat] = $localeFormat->format($now); } foreach ($this->formats['time'] as $timeFormat) { // Display format with current date to user $localeFormat->setPattern($timeFormat); $timeFormats[$timeFormat] = $localeFormat->format($now); } } $builder->add('username', 'text', array('constraints' => array(new Assert\NotBlank())))->add('email', 'email', array('constraints' => array(new Assert\NotBlank(), new Assert\Email())))->add('firstName', 'text', array('constraints' => array(new Assert\NotBlank())))->add('lastName', 'text', array('constraints' => array(new Assert\NotBlank())))->add('timezone', 'timezone')->add('avatarImage', new AvatarUploadType(), array('label' => 'Profile image')); if ($options['new']) { $builder->add('password', 'repeated', array('required' => true, 'type' => 'password', 'first_name' => 'password', 'second_name' => 'password_again', 'invalid_message' => 'The password fields must match.')); } // $builder // ->add('language', 'language', array( // 'data' => 'en_US', // 'disabled' => true // )) // ->add('locale', 'locale', array( // 'data' => 'en_US', // 'disabled' => true, // )) // ->add('currency', 'currency', array( // 'data' => 'USD', // 'disabled' => true // )) // ->add('dateFormat', 'choice', array( // 'data' => 'yyyy-MM-dd', // 'disabled' => true, // 'label' => 'Date Format', // 'choices' => $dateFormats, // 'multiple' => false, // )) // ->add('timeFormat', 'choice', array( // 'data' => 'HH:mm:ss', // 'disabled' => true, // 'label' => 'Time Format', // 'choices' => $timeFormats, // 'multiple' => false, // )); }
/** * {@inheritdoc} */ public function getPattern($dateType, $timeType, $locale = null) { if (!$locale) { $locale = $this->localeSettings->getLocale(); } if ($timeType !== \IntlDateFormatter::NONE) { $timeType = \IntlDateFormatter::SHORT; } $localeFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::SHORT, $timeType, null, \IntlDateFormatter::GREGORIAN); $pattern = $localeFormatter->getPattern(); return $this->modifyPattern($pattern, $timeType); }
/** * @param string $name * @param \IntlDateFormatter $formatter * * @return \Twig_SimpleFilter */ private function getIntlDateFilter($name, \IntlDateFormatter $formatter) { return new \Twig_SimpleFilter($name, function ($dateValue) use($formatter) { if ($dateValue instanceof \DateTimeInterface) { return $formatter->format($dateValue->getTimestamp()); } elseif (is_string($dateValue)) { return $formatter->format($dateValue); } else { return ''; } }); }
protected static function normalize($datetime, array $options) { assert($datetime instanceof \DateTime || $datetime === null); if ($datetime === null) { return null; } if (is_string($options['format'])) { return $datetime->format($options['format']); } $dateFormatter = new \IntlDateFormatter($options['locale'], self::getIntlDateFormat($options['date_format']), self::getIntlDateFormat($options['time_format'])); return $dateFormatter->format($datetime); }
/** * @param $datetime * @param null $format * @return string */ private function formatTimestamps($datetime, $format = null) { if (empty($datetime)) { return; } $dateFormat = is_int($format) ? $format : \IntlDateFormatter::MEDIUM; $timeFormat = \IntlDateFormatter::NONE; $calendar = \IntlDateFormatter::GREGORIAN; $pattern = is_string($format) ? $format : null; $formatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $datetime->getTimezone()->getName(), $calendar, $pattern); $formatter->setLenient(false); $timestamp = $datetime->getTimestamp(); return $formatter->format($timestamp); }
/** * Create pattern Date Javascript * * @param IntlDateFormatter $formatter * * @return string pattern date of Javascript */ protected function getJavascriptPattern(\IntlDateFormatter $formatter) { $pattern = $formatter->getPattern(); $patterns = preg_split('([\\\\/.:_;,\\s-\\ ]{1})', $pattern); $exits = array(); // Transform pattern for JQuery ui datepicker foreach ($patterns as $index => $val) { switch ($val) { case 'yy': $exits[$val] = 'y'; break; case 'y': case 'yyyy': $exits[$val] = 'yy'; break; case 'M': $exits[$val] = 'm'; break; case 'MM': case 'L': case 'LL': $exits[$val] = 'mm'; break; case 'MMM': case 'LLL': $exits[$val] = 'M'; break; case 'MMMM': case 'LLLL': $exits[$val] = 'MM'; break; case 'D': $exits[$val] = 'o'; break; case 'E': case 'EE': case 'EEE': case 'eee': $exits[$val] = 'D'; break; case 'EEEE': case 'eeee': $exits[$val] = 'DD'; break; } } return str_replace(array_keys($exits), array_values($exits), $pattern); }
function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null, $calendar = 'gregorian') { $date = twig_date_converter($env, $date, $timezone); $formatValues = array('none' => IntlDateFormatter::NONE, 'short' => IntlDateFormatter::SHORT, 'medium' => IntlDateFormatter::MEDIUM, 'long' => IntlDateFormatter::LONG, 'full' => IntlDateFormatter::FULL); $formatter = IntlDateFormatter::create($locale, $formatValues[$dateFormat], $formatValues[$timeFormat], PHP_VERSION_ID >= 50500 ? $date->getTimezone() : $date->getTimezone()->getName(), 'gregorian' === $calendar ? IntlDateFormatter::GREGORIAN : IntlDateFormatter::TRADITIONAL, $format); return $formatter->format($date->getTimestamp()); }
/** * {@inheritdoc} */ public function formatObject($object, $format = null, $locale = null) { if ($this->useIntlFormatObject) { return \IntlDateFormatter::formatObject($object, $format, $locale); } return $this->doFormatObject($object, $format, $locale); }
function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) { $date = twig_date_converter($env, $date, $timezone); $formatValues = array('none' => IntlDateFormatter::NONE, 'short' => IntlDateFormatter::SHORT, 'medium' => IntlDateFormatter::MEDIUM, 'long' => IntlDateFormatter::LONG, 'full' => IntlDateFormatter::FULL); $formatter = IntlDateFormatter::create($locale, $formatValues[$dateFormat], $formatValues[$timeFormat], $date->getTimezone()->getName(), IntlDateFormatter::GREGORIAN, $format); return $formatter->format($date->getTimestamp()); }
public function finishView(FormView $view, FormInterface $form, array $options) { parent::finishView($view, $form, $options); $format = $options['format']; if (is_int($format)) { $formatter = new \IntlDateFormatter(\Locale::getDefault(), $format, \IntlDateFormatter::NONE); $format = $formatter->getPattern(); } $view->vars['locale'] = \Locale::getDefault(); $view->vars['format_php'] = $format; if ($this->convertPhpFormatToJQueryUI('yyyy') == 'y') { die('nan'); } $format = $this->convertPhpFormatToJQueryUI($format); $view->vars['format_js'] = $format; }
/** * Renders grid column * * @param \Magento\Framework\Object $row * @return string */ public function render(\Magento\Framework\Object $row) { //@todo: check this logic manually if ($data = $row->getData($this->getColumn()->getIndex())) { switch ($this->getColumn()->getPeriodType()) { case 'month': $dateFormat = 'yyyy-MM'; break; case 'year': $dateFormat = 'yyyy'; break; default: $dateFormat = \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT; break; } $format = $this->_getFormat(); try { $data = $this->getColumn()->getGmtoffset() ? \IntlDateFormatter::formatObject($this->_localeDate->date(new \DateTime($data)), $format) : \IntlDateFormatter::formatObject($this->_localeDate->date(new \DateTime($data), null, false), $format); } catch (\Exception $e) { $data = $this->getColumn()->getTimezone() ? \IntlDateFormatter::formatObject($this->_localeDate->date(new \DateTime($data)), $format) : \IntlDateFormatter::formatObject($this->_localeDate->date(new \DateTime($data), null, false), $format); } return $data; } return $this->getColumn()->getDefault(); }
/** * @dataProvider getDayOfWeekNamesDataProvider */ public function testGetDayOfWeekNames($width, $locale, $defaultLocale = null) { $this->calendar->setLocale($locale); if (null !== $defaultLocale) { \Locale::setDefault($defaultLocale); } $actual = $this->calendar->getDayOfWeekNames($width); $this->assertCount(7, $actual); $widthToPatternMap = array(Calendar::WIDTH_ABBREVIATED => 'ccc', Calendar::WIDTH_SHORT => 'cccccc', Calendar::WIDTH_NARROW => 'ccccc', Calendar::WIDTH_WIDE => 'cccc'); $formatter = new \IntlDateFormatter($locale, null, null, 'UTC', \IntlDateFormatter::GREGORIAN, $widthToPatternMap[$width ?: Calendar::WIDTH_WIDE]); foreach ($actual as $dayNum => $dayName) { $checkDate = new \DateTime('2013/09/0' . $dayNum, new \DateTimeZone('UTC')); $expected = $formatter->format((int) $checkDate->format('U')); $this->assertEquals($expected, $actual[$dayNum], 'Incorrect day for day #' . $dayNum); } }