0

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.

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. 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

Owais Aslam
  • 1,577
  • 1
  • 17
  • 39

1 Answers1

2
{{ 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. 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:

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

And then do a little bit of JS scripting:

$('.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

Obviously, you don't need neither jQuery not MomentJS, but I used them here for convenience...

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85