As per http://php.net/manual/en/class.datetime.php
根据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.
PHP遵循“ATOM”日期格式。要生成像“2017-11-14”这样的日期,我们需要日期格式字符串“Y-m-d”。我能够使用php日期函数和类生成它。它按预期工作。
/////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
现在我想在Symfony3应用程序中动态使用这些日期格式。根据文档,dateType字段的格式必须符合“RFC3339”,因为它使用IntlDateFormatter类进行格式化。 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".
要在我的表单上呈现datepicker字段,我使用bootstrap datepicker(https://bootstrap-datepicker.readthedocs.io/en/stable/options.html#format)。格式需求在这里生成相同的前一个日期有点不同“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.
虽然我可以理解datepicker格式可以与PHP不同。但我无法理解为什么PHP和symfony日期格式要求之间存在差异。
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.
我正在考虑一些可以将PHP日期字符串转换为symfony和javascript格式的转换器,或者如果我可以设法更改symfony的dateformatter,它在我看来也有帮助。
1 个解决方案
#1
0
You are confusing the symfony format and html5 one. There is no symfony format, the rfc refers to the html specifications.
你混淆了symfony格式和html5格式。没有symfony格式,rfc指的是html规范。
You should, for your frontend:
你应该为你的前端:
- Create a twig filter that output any datetime in the user format preference
- Use a javascript date widget instead of html date input. Mainly because the format of html date widget can't be changed and you won't be able to use the user format preference of your application. It is the format that is given to the request that is at the RFC3339 format:
YYYY-mm-dd
创建一个twig过滤器,以用户格式首选项输出任何日期时间
使用javascript日期小部件而不是html日期输入。主要是因为html日期窗口小部件的格式无法更改,您将无法使用应用程序的用户格式首选项。它是以RFC3339格式提供给请求的格式:YYYY-mm-dd
That's all, your controllers and model should just handle Datetimes object
总而言之,您的控制器和模型应该只处理Datetimes对象
#1
0
You are confusing the symfony format and html5 one. There is no symfony format, the rfc refers to the html specifications.
你混淆了symfony格式和html5格式。没有symfony格式,rfc指的是html规范。
You should, for your frontend:
你应该为你的前端:
- Create a twig filter that output any datetime in the user format preference
- Use a javascript date widget instead of html date input. Mainly because the format of html date widget can't be changed and you won't be able to use the user format preference of your application. It is the format that is given to the request that is at the RFC3339 format:
YYYY-mm-dd
创建一个twig过滤器,以用户格式首选项输出任何日期时间
使用javascript日期小部件而不是html日期输入。主要是因为html日期窗口小部件的格式无法更改,您将无法使用应用程序的用户格式首选项。它是以RFC3339格式提供给请求的格式:YYYY-mm-dd
That's all, your controllers and model should just handle Datetimes object
总而言之,您的控制器和模型应该只处理Datetimes对象