From 32d571fb6342a63f77ae82ed62c264bfca9a3b2a Mon Sep 17 00:00:00 2001 From: Peter Rotich <peter@osticket.com> Date: Tue, 22 Dec 2015 05:56:36 +0000 Subject: [PATCH] bug: Set due date based on user's timezone --- include/class.misc.php | 32 ++++++++++++++++++++++++++------ include/class.ticket.php | 13 ++++++------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/include/class.misc.php b/include/class.misc.php index 69063c442..0de812b73 100644 --- a/include/class.misc.php +++ b/include/class.misc.php @@ -80,6 +80,28 @@ class Misc { return $dbtime - $dbtz->getOffset($D); } + // Take user's time and return GMT time. + function user2gmtime($timestamp=null, $user=null) { + global $cfg; + + $tz = new DateTimeZone($cfg->getTimezone($user)); + + if (!$timestamp) + $timestamp = 'now'; + + if (is_int($timestamp)) { + $time = $timestamp; + } else { + $date = new DateTime($timestamp, $tz); + $time = $date->format('U'); + } + + if (!($D = DateTime::createFromFormat('U', $time))) + return $time; + + return $time - $tz->getOffset($D); + } + //Take user time or gmtime and return db (mysql) time. function dbtime($var=null){ static $dbtz; @@ -88,13 +110,11 @@ class Misc { if (is_null($var) || !$var) { // Default timezone is set to UTC $time = time(); + } else { + // User time to UTC + $time = self::user2gmtime($var); } - else { //user time to UTC - $tz = new DateTimeZone($cfg->getTimezone()); - $time = is_int($var) ? $var : strtotime($var); - $D = DateTime::createFromFormat('U', $time); - $time -= $tz->getOffset($D); - } + if (!isset($dbtz)) { $dbtz = new DateTimeZone($cfg->getDbTimezone()); } diff --git a/include/class.ticket.php b/include/class.ticket.php index 61da381c5..da7f9bdf1 100644 --- a/include/class.ticket.php +++ b/include/class.ticket.php @@ -2739,8 +2739,7 @@ implements RestrictedAccess, Threadable { $errors['time']=__('Select a time from the list'); elseif (strtotime($vars['duedate'].' '.$vars['time']) === false) $errors['duedate']=__('Invalid due date'); - // FIXME: Using time() violates database and user timezone - elseif (strtotime($vars['duedate'].' '.$vars['time']) <= time()) + elseif (Misc::user2gmtime($vars['duedate'].' '.$vars['time']) <= Misc::user2gmtime()) $errors['duedate']=__('Due date must be in the future'); } @@ -3090,13 +3089,13 @@ implements RestrictedAccess, Threadable { if(!Validator::process($fields, $vars, $errors) && !$errors['err']) $errors['err'] =__('Missing or invalid data - check the errors and try again'); - //Make sure the due date is valid - if($vars['duedate']) { - if(!$vars['time'] || strpos($vars['time'],':')===false) + // Make sure the due date is valid + if ($vars['duedate']) { + if (!$vars['time'] || strpos($vars['time'],':') === false) $errors['time']=__('Select a time from the list'); - elseif(strtotime($vars['duedate'].' '.$vars['time'])===false) + elseif (strtotime($vars['duedate'].' '.$vars['time']) === false) $errors['duedate']=__('Invalid due date'); - elseif(strtotime($vars['duedate'].' '.$vars['time'])<=time()) + elseif (Misc::user2gmtime($vars['duedate'].' '.$vars['time']) <= Misc::user2gmtime()) $errors['duedate']=__('Due date must be in the future'); } -- GitLab