本文整理汇总了PHP中IntlDateFormatter::format方法的典型用法代码示例。如果您正苦于以下问题:PHP IntlDateFormatter::format方法的具体用法?PHP IntlDateFormatter::format怎么用?PHP IntlDateFormatter::format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IntlDateFormatter
的用法示例。
在下文中一共展示了IntlDateFormatter::format方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getDateFormatted
/**
* @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 '';
}
}
示例2: getIntlDateFilter
/**
* @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 '';
}
});
}
示例3: buildForm
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,
// ));
}
示例4: query
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;
}
示例5: format
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;
}
示例6: buildView
/**
* {@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;
}
示例7: formatValue
/**
* 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;
}
示例8: d
function d(IntlDateFormatter $df)
{
global $ts;
echo $df->format($ts), "\n";
var_dump($df->getTimeZoneID(), $df->getTimeZone()->getID());
echo "\n";
}
示例9: openfit_base_preprocess_comment
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>';
}
示例10: createView
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']);
}
示例11: d
function d(IntlDateFormatter $df)
{
global $ts;
echo $df->format($ts), "\n";
var_dump($df->getCalendar(), $df->getCalendarObject()->getType(), $df->getCalendarObject()->getTimeZone()->getId());
echo "\n";
}
示例12: __invoke
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);
}
示例13: format
/**
* {@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);
}
示例14: formatDate
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']);
}
示例15: ruDateFormat
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);
}