处理Symfony2中的本地化DateTime

时间:2022-10-22 16:45:28

I am working on a Symfony2 based website where client can request users to post at a particular time of the day for exemple from 9pm to 10pm.

我正在开发一个基于Symfony2的网站,客户可以在晚上9点到晚上10点要求用户在当天的特定时间发布。

Users are coming from all over the world and if a client request a post at 9pm, it s not based on his local time but on the user that will post for him.

用户来自世界各地,如果客户在晚上9点要求发帖,则不是基于他当地时间,而是基于将为他发布的用户。

Should i request my user what is his timezone and store it and then use it to convert the php DateTime of his post to his time zone and then check if he posted it at 9pm ? Could you show me a little code exemple for that ?

我应该向我的用户请求他的时区是什么并存储它然后用它将他的帖子的php DateTime转换为他的时区,然后检查他是否在晚上9点发布了它?你能告诉我一些代码吗?

Also, what is the good way to ask a user his timezone ?

另外,询问用户他的时区的好方法是什么?

Edit : I want to ask the user his timezone and not get it programmatically because its actually his audiance timezone ( people who see his posts ) and not his personnal timezone.

编辑:我想问用户他的时区而不是以编程方式得到它,因为它实际上是他的听众时区(看到他的帖子的人)而不是他的个人时区。

1 个解决方案

#1


0  

best way is to use js timezone detection with library jsTimezoneDetect

最好的方法是使用js时区检测库jsTimezoneDetect

next send via ajax request timezone to save in session

接下来通过ajax请求时区发送以保存在会话中

next use timezone in session to set timezone to date, you probably want to make twig extension for that so you can use it easily in twig. 1. all your dates must be in same timezone, for example UTC

接下来在会话中使用时区来设置时区到目前为止,您可能希望为此设置枝条扩展,以便您可以轻松地在树枝中使用它。 1.您的所有日期必须在同一时区,例如UTC

you can set it in AppKernel.php

你可以在AppKernel.php中设置它

date_default_timezone_set('UTC');
  1. to display date use something like this

    显示日期使用这样的东西

    $date->setTimezone(new \DateTimeZone($timezone));

where date is \DateTime object

其中date是\ DateTime对象


if you want to ask user anyways you can create form with: you create a form, use http://symfony.com/doc/current/reference/forms/types/timezone.html field and send that timezone to controller (AJAX or standard), set session with timezone, and for the rest handle it like like I described above by displaying date via your custom twig extension etc.

如果您想要询问用户,您可以创建表单:创建表单,使用http://symfony.com/doc/current/reference/forms/types/timezone.html字段并将该时区发送给控制器(AJAX或标准),设置与时区的会话,其余处理就像我上面描述的那样通过自定义树枝扩展等显示日期。

#1


0  

best way is to use js timezone detection with library jsTimezoneDetect

最好的方法是使用js时区检测库jsTimezoneDetect

next send via ajax request timezone to save in session

接下来通过ajax请求时区发送以保存在会话中

next use timezone in session to set timezone to date, you probably want to make twig extension for that so you can use it easily in twig. 1. all your dates must be in same timezone, for example UTC

接下来在会话中使用时区来设置时区到目前为止,您可能希望为此设置枝条扩展,以便您可以轻松地在树枝中使用它。 1.您的所有日期必须在同一时区,例如UTC

you can set it in AppKernel.php

你可以在AppKernel.php中设置它

date_default_timezone_set('UTC');
  1. to display date use something like this

    显示日期使用这样的东西

    $date->setTimezone(new \DateTimeZone($timezone));

where date is \DateTime object

其中date是\ DateTime对象


if you want to ask user anyways you can create form with: you create a form, use http://symfony.com/doc/current/reference/forms/types/timezone.html field and send that timezone to controller (AJAX or standard), set session with timezone, and for the rest handle it like like I described above by displaying date via your custom twig extension etc.

如果您想要询问用户,您可以创建表单:创建表单,使用http://symfony.com/doc/current/reference/forms/types/timezone.html字段并将该时区发送给控制器(AJAX或标准),设置与时区的会话,其余处理就像我上面描述的那样通过自定义树枝扩展等显示日期。