0

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.

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.

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 ?

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.

  • Perhaps these could be of help http://stackoverflow.com/questions/20743060/symfony2-and-date-default-timezone-get-it-is-not-safe-to-rely-on-the-system and http://epicfoobar.com/2013/10/how-to-set-time-zone-in-symfony2/ – Shairyar Mar 09 '15 at 04:30
  • @Baig Thank you for your help it still dont help me to find a way to ask my users their time zone the good way – Adel 'Sean' Helal Mar 09 '15 at 07:55
  • is there a profile page of user? if yes what information are you asking them there? may be you want to add another field there of timezone. – Shairyar Mar 09 '15 at 08:00
  • 1
    @Baig yes its exactly that ! LPodolski just gave me the right ressouce in his answer to do it thank you for your help and your time my friend ! – Adel 'Sean' Helal Mar 09 '15 at 17:01

1 Answers1

0

best way is to use js timezone detection with library jsTimezoneDetect

next send via ajax request timezone to save in session

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

you can set it in 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


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.

LPodolski
  • 2,888
  • 4
  • 21
  • 24
  • Ok i get it but i miss one point, if i want to ask the user his timezone instead of detecting it programmatically ? Is there a way to do that simply in stead of listing all the timze available manualy and creating a form to let the user pick up his time zone ? – Adel 'Sean' Helal Mar 09 '15 at 07:53
  • @Adel'Sean'Helal yes, i updated my answer, you can find it at bottom – LPodolski Mar 09 '15 at 08:12
  • THis is exactly what i need ! thank you for your time ! i feel ashamed i didnt see that in the doc... Thank you again ! – Adel 'Sean' Helal Mar 09 '15 at 17:02