0

As per http://php.net/manual/en/class.datetime.php

PHP follows "ATOM" date format. To produce a date like "2017-11-14" we need date format string "Y-m-d". I am able to produce it using php date function and class. It works as expected.

/////Using date function////
echo date("Y-m-d");

/////Using DateTime class////
$date = new DateTime();
echo $date->format('Y-m-d');

Now I want to use these date formats dynamically in my Symfony3 application. As per docs, format for dateType field must be according to "RFC3339" because it uses IntlDateFormatter class for formatting. https://symfony.com/doc/current/reference/forms/types/date.html#format

So to produce date same as previous one, we need different format string

$date = new DateTime();
$dateFormat = new IntlDateFormatter(
    "en_US",
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    'America/Los_Angeles',
    IntlDateFormatter::GREGORIAN,
    'yyyy-MM-dd'
);
echo $dateFormat->format($date->getTimestamp());

To render datepicker fields on my forms, I am using bootstrap datepicker (https://bootstrap-datepicker.readthedocs.io/en/stable/options.html#format). Format demand to produce same previous date here is bit different "yyyy-mm-dd".

Although I can understand datepicker format can be different from PHP one. But I am unable to understand why there is difference between PHP and symfony date format requirements.

I am not sure how to make it working, as there can be plenty of formats, user can choose from. So a user chooses his date format preferences and I need to produce same date string everywhere.

I am thinking about some converter which can transform PHP date string to symfony and javascript formats or if I can manage to change dateformatter for symfony it can help also in my opinion.

4

1 回答 1

0

您混淆了 symfony 格式和 html5 格式。没有 symfony 格式,rfc 指的是 html 规范。

对于您的前端,您应该:

  1. 创建一个树枝过滤器,以用户格式首选项输出任何日期时间
  2. 使用 javascript 日期小部件而不是 html 日期输入。主要是因为html 日期小部件的格式无法更改,您将无法使用应用程序的用户格式首选项。它是以 RFC3339 格式提供给请求的格式:YYYY-mm-dd

就是这样,您的控制器和模型应该只处理 Datetimes 对象

于 2017-11-14T11:03:15.767 回答