/** * {@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; }
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)); }
/** * {@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 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); }
public function getPattern() { // set order as specified in the pattern if ($this->getOption('pattern')) { return $this->getOption('pattern'); } // set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy) // lookup various formats at http://userguide.icu-project.org/formatparse/datetime if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $this->formatter->getPattern())) { return preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $this->formatter->getPattern()); } // default fallback return '{{ year }}-{{ month }}-{{ day }}'; }
/** * 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); }
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; }
/** * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $format = function (Options $options, $value) { if ($options['widget'] === 'single_text') { $formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE); $format = $formatter->getPattern(); $this->replaceInString('yy', 'yyyy', $format); $this->replaceInString('d', 'dd', $format); return $format; } return $value; }; $resolver->setDefaults(array('format' => $format, 'language' => \Locale::getDefault(), 'leading_zero' => false)); $resolver->setOptional(array('placeholder', 'language', 'leading_zero')); $resolver->setAllowedTypes(array('placeholder' => array('string'), 'language' => array('string'), 'leading_zero' => array('bool'))); }
public function get_livedit() { $data = new DateTime(); $locale = 'it-it'; $fmt = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); echo $fmt->format($data) . '<br>'; echo 'pattern is ' . $fmt->getPattern() . '<br>'; $locale = 'it_CH'; $fmt = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); echo $fmt->format($data) . '<br>'; echo 'pattern ' . $fmt->getPattern() . '<br>'; $locale = 'en-US'; $fmt = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); echo $fmt->format($data) . '<br>'; echo 'pattern ' . $fmt->getPattern() . '<br>'; die('NO'); }
/** * {@inheritDoc} */ public function render(array $attributes = array()) { if ($this->getOption('widget') === self::INPUT) { return $this->generator->tag('input', array_merge(array('id' => $this->getId(), 'name' => $this->getName(), 'value' => $this->getDisplayedData(), 'type' => 'text'), $attributes)); } else { // set order as specified in the pattern if ($this->getOption('pattern')) { $pattern = $this->getOption('pattern'); } else { if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $this->formatter->getPattern())) { $pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('%year%', '%month%', '%day%'), $this->formatter->getPattern()); } else { $pattern = '%year%-%month%-%day%'; } } return str_replace(array('%year%', '%month%', '%day%'), array($this->get('year')->render($attributes), $this->get('month')->render($attributes), $this->get('day')->render($attributes)), $pattern); } }
private function initPattern() { $formatter = new IntlDateFormatter($this->localeString, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); $pattern = $formatter->getPattern(); $properties = array('dateFieldDelimiter' => NULL, 'MDY_dayPosition' => NULL, 'MDY_monthPosition' => NULL, 'MDY_yearPosition' => NULL); $position = 0; for ($i = 0; $i < strlen($pattern); ++$i) { switch (substr($pattern, $i, 1)) { case 'y': case 'Y': case 'u': if (NULL === $properties['MDY_yearPosition']) { $properties['MDY_yearPosition'] = ++$position; } break; case 'M': case 'L': if (NULL === $properties['MDY_monthPosition']) { $properties['MDY_monthPosition'] = ++$position; } break; case 'd': if (NULL === $properties['MDY_dayPosition']) { $properties['MDY_dayPosition'] = ++$position; } break; case ' ': // ignore break; default: if (NULL === $properties['dateFieldDelimiter']) { $properties['dateFieldDelimiter'] = substr($pattern, $i, 1); } break; } } $this->properties = array_merge($this->properties, $properties); }
private function formatTimestamps(\IntlDateFormatter $formatter, $regex, array $timestamps) { $pattern = $formatter->getPattern(); $timezone = $formatter->getTimezoneId(); if (version_compare(\PHP_VERSION, '5.5.0-dev', '>=')) { $formatter->setTimeZone(\DateTimeZone::UTC); } else { $formatter->setTimeZoneId(\DateTimeZone::UTC); } if (preg_match($regex, $pattern, $matches)) { $formatter->setPattern($matches[0]); foreach ($timestamps as $key => $timestamp) { $timestamps[$key] = $formatter->format($timestamp); } // I'd like to clone the formatter above, but then we get a // segmentation fault, so let's restore the old state instead $formatter->setPattern($pattern); } if (version_compare(\PHP_VERSION, '5.5.0-dev', '>=')) { $formatter->setTimeZone($timezone); } else { $formatter->setTimeZoneId($timezone); } return $timestamps; }
private function formatTimestamps(\IntlDateFormatter $formatter, $regex, array $timestamps) { $pattern = $formatter->getPattern(); $timezone = $formatter->getTimezoneId(); if ($setTimeZone = method_exists($formatter, 'setTimeZone')) { $formatter->setTimeZone('UTC'); } else { $formatter->setTimeZoneId('UTC'); } if (preg_match($regex, $pattern, $matches)) { $formatter->setPattern($matches[0]); foreach ($timestamps as $key => $timestamp) { $timestamps[$key] = $formatter->format($timestamp); } // I'd like to clone the formatter above, but then we get a // segmentation fault, so let's restore the old state instead $formatter->setPattern($pattern); } if ($setTimeZone) { $formatter->setTimeZone($timezone); } else { $formatter->setTimeZoneId($timezone); } return $timestamps; }
/** * Get the pattern used for the IntlDateFormatter * * @param int|string $dateType Constant of IntlDateFormatter (NONE, FULL, LONG, MEDIUM, SHORT) or it's string name * @param int|string $timeType Constant IntlDateFormatter (NONE, FULL, LONG, MEDIUM, SHORT) or it's string name * @param string|null $locale * @return string */ public function getPattern($dateType, $timeType, $locale = null) { if (!$locale) { $locale = $this->localeSettings->getLocale(); } if (null === $dateType) { $dateType = static::DEFAULT_DATE_TYPE; } if (null === $timeType) { $timeType = static::DEFAULT_TIME_TYPE; } $dateType = $this->parseDateType($dateType); $timeType = $this->parseDateType($timeType); $localeFormatter = new \IntlDateFormatter($locale, $dateType, $timeType, null, \IntlDateFormatter::GREGORIAN); return $localeFormatter->getPattern(); }
private function formatTimestamps(\IntlDateFormatter $formatter, $regex, array $timestamps) { $pattern = $formatter->getPattern(); $timezone = $formatter->getTimezoneId(); $formattedTimestamps = array(); $formatter->setTimeZone('UTC'); if (preg_match($regex, $pattern, $matches)) { $formatter->setPattern($matches[0]); foreach ($timestamps as $timestamp => $choice) { $formattedTimestamps[$formatter->format($timestamp)] = $choice; } // I'd like to clone the formatter above, but then we get a // segmentation fault, so let's restore the old state instead $formatter->setPattern($pattern); } $formatter->setTimeZone($timezone); return $formattedTimestamps; }
private function formatMonths(\IntlDateFormatter $formatter, array $months) { $pattern = $formatter->getPattern(); $timezone = $formatter->getTimezoneId(); $formatter->setTimezoneId(\DateTimeZone::UTC); if (preg_match('/M+/', $pattern, $matches)) { $formatter->setPattern($matches[0]); foreach ($months as $key => $value) { $months[$key] = $formatter->format(gmmktime(0, 0, 0, $key, 15)); } // I'd like to clone the formatter above, but then we get a // segmentation fault, so let's restore the old state instead $formatter->setPattern($pattern); } $formatter->setTimezoneId($timezone); return $months; }
/** * Gets the date format for the current locale, using "dd" for days, "mm" for months, "yyyy" * for years, and "/" as separator (i.e: dd/mm/yyyy or yyyy/mm/dd). */ public function getDateFormatForCurrentLocale() { // get basic format $df = new \IntlDateFormatter($this->locale, \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE); $fmt = $df->getPattern(); // apply needed transformations. $fmt = strtolower($fmt); if (substr_count($fmt, "m") == 1) { $fmt = str_ireplace("m", "mm", $fmt); } if (substr_count($fmt, "d") == 1) { $fmt = str_ireplace("d", "dd", $fmt); } if (substr_count($fmt, "y") == 1) { $fmt = str_ireplace("y", "yyyy", $fmt); } else { if (substr_count($fmt, "y") == 2) { $fmt = str_ireplace("yy", "yyyy", $fmt); } } return $fmt; }
/** * Return date pattern for given date format * @param string|int $format * @return string */ protected function getPattern($format) { if (is_int($format)) { switch ($format) { case \IntlDateFormatter::FULL: case \IntlDateFormatter::LONG: case \IntlDateFormatter::MEDIUM: case \IntlDateFormatter::SHORT: $formatter = new \IntlDateFormatter(\Locale::getDefault(), (int) $format, \IntlDateFormatter::NONE); $format = $formatter->getPattern(); break; default: break; } } return $format; }
protected function getPattern($locale, $dateType, $timeType) { $localeFormatter = new \IntlDateFormatter($locale, $dateType, $timeType, null, \IntlDateFormatter::GREGORIAN); return $localeFormatter->getPattern(); }
protected function getIntlDateFormatter() : \IntlDateFormatter { $intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $this->dateFormat, $this->timeFormat, $this->outputTimezone, $this->calendar, $this->pattern); $intlDateFormatter->setLenient(false); // Ensure the year is always four digits when no pattern is set if ('' === $this->pattern) { $intlDateFormatter->setPattern(str_replace(['yy', 'yyyyyyyy'], 'yyyy', $intlDateFormatter->getPattern())); } return $intlDateFormatter; }