且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony buildForm将日期时间转换为本地并返回utc

更新时间:2023-02-03 17:24:51

您为此过于复杂了.Symfony2表单已经很好地涵盖了这一点.

You're overcomplicating this. Symfony2 forms already covers this nicely.

请参阅:

模型时区: http://symfony.com/doc/current/reference/forms/types/date.html#model-timezone

查看时区: http://symfony.com/doc/current/reference/forms/types/date.html#view-timezone

正确使用这些可以解决您的问题,而无需任何其他代码.

Used correctly these solve your issue without any extra code needed.

您可以将用户细分(或用于推断正确时区的任何数据)注入表单中,并使用该用户细分中的时区在日期字段中设置view_timezone.无论如何,沿着这些路线:)

You could inject your user segment (or whatever data you use to deduce which is the correct time zone) into your form and use the timezone on that user segment to set view_timezone on your date field. Something along those lines anyway :)