TransWikia.com

Allow user to set timezone from within Craft when entering a time in a date/time field

Craft CMS Asked by kenyabob on August 10, 2021

We are creating a small site to manage meetings that take place in many timezones. Administrators of the site will create meetings on behalf of teams everywhere. They may be in pacific timezone and receive a request to create a meeting from a team in England.

When creating the entry, the date/time they see if for pacific timezone, and to create the event with the correct time for GMT+1, they must do the math in their head as to what time in PST corresponds to the correct time in GMT+1.

Is it possible to have or add a field to specify which timezone the provided time is in, vs relying on the timezone set in settings?

One Answer

I solved that by adding a dropdown field to user profiles where I allowed users to select timezone and then in custom module set the selected timezone like this:

class SiteModule extends Module
{
   public function __construct($id, $parent = null, array $config = [])
   {
       // Set correct Timezone
       $currentUser = Craft::$app->getUser();
       if ($currentUser) {
           $user = $currentUser->getIdentity();
           $timezone = null;
           if ($user->userTimeZone->value) $timezone = $user->userTimeZone->value;
           if ($timezone) {
               Craft::$app->setTimeZone($timezone);
           }
       }

       ...

If user is not logged in or does not have a timezone set Craft will use the system timezone for those users.

Timezone dropdown values are in the form 'America/Chicago'.

Answered by Mato on August 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP