Question

I am trying to set the Start Date & Time automatically to the users current date and time when logging a call. This is to be set prior to loading the view.

The code is working but it is not taking into account my time zone, it is an hour behind and in uk time GMT.

How do I ensure the time zone is correct.

class CustomCallsController extends SugarController
{
    public function action_log_inbound()
{

    global $timedate;

    if(!isset($timedate) || empty($timedate))
    {
        $timedate = new TimeDate();
    }

    // get user for calculation
    $user = (empty($user)) ? $this->user : $user;

    $this->bean->direction = 'Inbound'; 
    $this->bean->date_start = $timedate->asUser($timedate->getNow(), $user);


    echo $this->bean->date_start;

    $this->view = 'edit';   
 }
}
Était-ce utile?

La solution

The above code is correct, I had not set the time zone correctly in the user settings. It was set to GMT not GMT +1

Autres conseils

Make use of $timedate->to_display_date_time

global $timedate;
$timedate->to_display_date_time($timedate->nowDb());

In most cases when there is a need to reference to a user actually creating/updating/deleting/etc. record you may use

global $current_user; 

and then your code may look like:

$this->bean->date_start = $timedate->asUser($timedate->getNow(), $current_user);

I have searched lot to change the SugarCRM default GMT timezone. When i create a new lead, the created date and modified date will update with GMT time. At last i have changed Sugar core files to solve the issue.

I have changed below function present in /include/SugarDateTime.php file. Now lead creating with my custom timezone.

public function setTimezone ($timezone)
    {
        // my custom timezone
        $timezone = "America/Los_Angeles";
        parent::setTimezone($timezone);
        return $this;
    }

Note: It will reflect those areas where this setTimezone function is calling. It worked for me, when i am creating a new lead.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top