0

Am trying to store localized time of the user via a date field in a channel form. My server timezone is set to GMT.

Wanted to display the time and date field to the non-logged in users in their timezone automatically on the frontend.

Am using EE 2.11.9 Would prefer to set the time zone for the users who will post entries during registration.

Probably get the location of the visitors by browser prompt or are there other elegant ways to do that?

Any pointers as to how I can accomplish that.

2
  • To help people answer your question could you provide some information about the version of EE you are using? It would also be helpful to know how you plan to determine the timezone of the users visiting your site. Thanks! Commented Mar 30, 2020 at 10:27
  • 1
    I have updated the question @JCOGS Design
    – Noor M
    Commented Apr 1, 2020 at 1:52

1 Answer 1

1

AFAIK there is no automatic way to do this kind of time zone adjustment in EE.

EE stores times relative to the server's location timezone, or relative a different timezone if you specify it in the CP. Dynamically modifying the server's default timezone on the fly is not a sensible option, so whatever you do will require you to modify the times shown to the user relative to the stored 'EE server time' for the event.

In light of this you have two options: dynamically modifying the displayed value in php or in javascript.

  • In php you can do this using the DateTimeZone object - once you know what timezone you want to use, you'll need code something like this to set the value
    $localTimezone = new DateTimeZone($time_zone_code);
    $displayDateTime = new DateTime('NOW', $localTimezone);
  • In Javascript you can do this using the javascript Date() class getTimezoneOffset method to get the difference in minutes between UTC time and the local time for the browser running the script - something like:
    var localTime = new Date();
    var localTimeZoneDifference = localTime.getTimezoneOffset();

Depending on what option you choose, you could then store either the $time_zone_code (e.g. 'GMT') value or localTimeZoneDifference value (e.g. 120) in a field linked to your user.

Whichever option you choose you'll still need to do more work to actually deploy the approach to give you the time outputs you want. Javascript might be a better option as the browser already knows about time zone differences through the Date() class. To use php you'd need to use an add-on to get you the time zone information. If you ask the authors there might be an EE2 version of the IP Geo Locator plugin available, or if not you maybe can rig up something that uses the EE IP to Nation plugin.

HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.