Sugarcrm datetime displaying in user timezone

243 Views Asked by At

I am facing a challenge.

  • I am using a datetime field.
  • I need the datetime to display after save without automatically changing based on user's time zone.

For example, if 7:30PM is entered, I need 7:30PM to always display regardless of user timezone.

In this example, all users should see 7:30PM regardless of their time zone.

This is critical in order for related logic and functionality to work as expected.

Does anyone have any idea how to skirt the auto time zone adjustment? I am looking for a javascript example to control the time zone on display.

Alternatively, if anyone has a suggestion for displaying a nice datetime picker and not storing the data in UTC this would be helpful in this case. I am tempted to use separate fields and a custom layout but if this can with the datetime field this would be better.

1

There are 1 best solutions below

0
Luke Attard On

I know this is an old post, but it seems to still get views.

Please note, this is not recommended practice, and should only be used as a last resort.

edit file include/MySugar/javascript/MySugar.js add at the bottom:

Date.prototype.getTimezoneOffset = () =>  { return 0; }

This creates an Override of the default function getTimezoneOffset to always return 0 offset.

Then edit file include/TimeDate.php

find the function adjustmentForUserTimeZone for me it was line 1957

change the value for all returns to be 0

from:

public function adjustmentForUserTimeZone()
    {
        $tz = $this->_getUserTZ();
        $server_tz = new DateTimeZone(date_default_timezone_get());
        if ($tz && $server_tz) {
            return ($server_tz->getOffset($this->now) - $tz->getOffset($this->now)) / 60;
        }

        return 0;
    }

to:

public function adjustmentForUserTimeZone()
    {
        $tz = $this->_getUserTZ();
        $server_tz = new DateTimeZone(date_default_timezone_get());
        if ($tz && $server_tz) {
            return 0;
        }

        return 0;
    }

This changes the server end to always use the server time as the time to display.