简体   繁体   English

Symfony 3从用户那里获取时区,而无需以表格形式询问他们

[英]Symfony 3 Get the timezone from users without asking them in a form

I have an announce website, at the moment, I have a Subscribe form who ask the user to give me his timezone 我有一个公告网站,此刻,我有一个“订阅”表单,要求用户给我他的时区

$builder->add('timezone', ChoiceType::class, array(
                'choices' => $choices
            ));

but I would like to change and take this information directly without asking the user in a form. 但我想直接更改并获取此信息,而无需以表格形式询问用户。

It's important for me because I will have a lot of users who will certainly not know their timezone and will choose a bad one. 这对我很重要,因为我会有很多用户,他们肯定不知道他们的时区,并且会选择一个不好的时区。

Actually, I have announced who is the store with a date in GMT timezone but I would like to display them in the timezone from someone who is registered or not on my website. 实际上,我已经宣布了谁是具有GMT时区日期的商店,但是我想在时区中显示那些在我的网站上注册或未登录的的商店。

For display i have two function: 为了显示,我有两个功能:

{{ clanwar.rendezvous|localizeddate('full', 'short') }}
{{ clanwar.rendezvous|date("d F Y, G:i e P", user.timezone)}}

The first one gives a date with the default timezone and it takes the timezone from the server. 第一个给出一个具有默认时区的日期,并从服务器获取该时区。 (i can change by defining it manually) The second one needs to have a timezone from a registered user. (我可以通过手动定义进行更改)。第二个需要具有注册用户的时区。

So both option can't be used for what I want to do. 所以这两个选项都不能用于我想做的事情。

I think I need to use a Javascript function. 我想我需要使用Javascript函数。 I would like to store the user timezone in my session and calculated all date with this information. 我想将用户时区存储在我的会话中,并使用此信息计算所有日期。 But I didn't find a good function who can just find this element. 但是我找不到一个可以找到此元素的好的函数。

Do you know something who can help me? 你知道有人可以帮助我吗? Thanks 谢谢

{{ clanwar.rendezvous|date("d F Y, G:i e P", user.timezone)}}

In this way you clearly would need to store the timezone in a User entity or something like that. 这样,您显然需要将时区存储在User实体或类似的实体中。 But this is prone to change, as user can tend to travel/move they might not be so disciplined when it comes to changing the timezone each time. 但这很容易改变,因为用户可能会旅行/移动,因此每次更改时区时可能不会受到严格的限制。

What I would suggest to is to display ISO formatted date in UTC in a, for example, <div> element with a special class: 我建议在UTC中以例如特殊类的<div>元素显示ISO格式的日期:

<div class="user_timezone_container">{{ clanwar.rendezvous|localizeddate('full', 'short') }}</div>

And then do a little bit of JS scripting: 然后做一点JS脚本:

$('.user_timezone_container').each(function(){
    var m = moment($(this).text()); //example: "2018-04-03T09:30:26Z"

    $(this).text(m.local().format('YYYY-MM-DD HH:mm:ss'));
})

I pieced the example above from the SO question Moment Js UTC to Local Time 我将上面的示例从SO问题Moment Js UTC转换为Local Time

Obviously, you don't need neither jQuery not MomentJS , but I used them here for convenience... 显然,您既不需要jQuery也不需要MomentJS ,但是为了方便起见,在这里使用了它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM