diff --git a/htdocs/admin/system/dolibarr.php b/htdocs/admin/system/dolibarr.php index 033432ccf2a..bb20516a3d5 100644 --- a/htdocs/admin/system/dolibarr.php +++ b/htdocs/admin/system/dolibarr.php @@ -157,12 +157,13 @@ $txt =$langs->trans("OSTZ").' (variable system TZ): '.($_ENV["TZ"]?$_ENV["TZ"]:$ $txt.=$langs->trans("PHPTZ").' (php.ini date.timezone): '.(ini_get("date.timezone")?ini_get("date.timezone"):$langs->trans("NotDefined")).''."\n"; // date.timezone must be in valued defined in http://fr3.php.net/manual/en/timezones.europe.php $var=!$var; print ''.$langs->trans("CurrentTimeZone").''; // Timezone server PHP -$a=getCurrentTimeZoneString(); -$a.=' '.getCurrentTimeZoneInt(); -$a.=' ('.(getCurrentTimeZoneInt()>0?'+':'').(getCurrentTimeZoneInt()*3600).')'; +$a=getServerTimeZoneString(); +$a.=' '.getServerTimeZoneInt(); +$a.=' ('.(getServerTimeZoneInt()>0?'+':'').(getServerTimeZoneInt()*3600).')'; print $form->textwithtooltip($a,$txt,2,1,img_info('')); print ''."\n"; // value defined in http://fr3.php.net/manual/en/timezones.europe.php $var=!$var; +print dol_now(); print '  => '.$langs->trans("CurrentHour").''.dol_print_date(dol_now(),'dayhour','tzserver').''."\n"; $var=!$var; print '  => dol_print_date(0,"dayhourtext")'.dol_print_date(0,"dayhourtext").''; diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index d056582f714..0788b3a0eec 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -128,7 +128,7 @@ $form=new Form($db); $companystatic=new Societe($db); $contactstatic=new Contact($db); -$now=dol_now('tzref'); +$now=dol_now(); // Define list of all external calendars $listofextcals=array(); diff --git a/htdocs/contrat/class/contrat.class.php b/htdocs/contrat/class/contrat.class.php index 9eca94bd8c0..f93f8d22d88 100644 --- a/htdocs/contrat/class/contrat.class.php +++ b/htdocs/contrat/class/contrat.class.php @@ -1587,7 +1587,7 @@ class ContratLigne */ function getLibStatut($mode) { - return $this->LibStatut($this->statut,$mode,(isset($this->date_fin_validite)?($this->date_fin_validite < dol_now('tzref')?1:0):-1)); + return $this->LibStatut($this->statut,$mode,(isset($this->date_fin_validite)?($this->date_fin_validite < dol_now()?1:0):-1)); } /** diff --git a/htdocs/contrat/index.php b/htdocs/contrat/index.php index d0045156eed..9ca6a240e50 100644 --- a/htdocs/contrat/index.php +++ b/htdocs/contrat/index.php @@ -102,7 +102,7 @@ $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c"; if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE cd.fk_contrat = c.rowid AND c.fk_soc = s.rowid"; -$sql.= " AND (cd.statut != 4 OR (cd.statut = 4 AND (cd.date_fin_validite is null or cd.date_fin_validite >= '".$db->idate(dol_now('tzref'))."')))"; +$sql.= " AND (cd.statut != 4 OR (cd.statut = 4 AND (cd.date_fin_validite is null or cd.date_fin_validite >= '".$db->idate($now)."')))"; $sql.= " AND c.entity = ".$conf->entity; if ($user->societe_id) $sql.=' AND c.fk_soc = '.$user->societe_id; if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; @@ -139,7 +139,7 @@ $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c"; if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE cd.fk_contrat = c.rowid AND c.fk_soc = s.rowid"; -$sql.= " AND (cd.statut = 4 AND cd.date_fin_validite < '".$db->idate(dol_now('tzref'))."')"; +$sql.= " AND (cd.statut = 4 AND cd.date_fin_validite < '".$db->idate($now)."')"; $sql.= " AND c.entity = ".$conf->entity; if ($user->societe_id) $sql.=' AND c.fk_soc = '.$user->societe_id; if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; diff --git a/htdocs/contrat/services.php b/htdocs/contrat/services.php index 24a788f8fd7..0d53b117d2d 100644 --- a/htdocs/contrat/services.php +++ b/htdocs/contrat/services.php @@ -64,7 +64,7 @@ $companystatic=new Societe($db); * View */ -$now=dol_now('tzref'); +$now=dol_now(); $form=new Form($db); diff --git a/htdocs/core/boxes/box_services_expired.php b/htdocs/core/boxes/box_services_expired.php index 4b2e9a857aa..005f1b0f863 100644 --- a/htdocs/core/boxes/box_services_expired.php +++ b/htdocs/core/boxes/box_services_expired.php @@ -66,7 +66,7 @@ include_once(DOL_DOCUMENT_ROOT."/core/boxes/modules_boxes.php"); $this->max=$max; - $now=dol_now('tzref'); + $now=dol_now(); $this->info_box_head = array('text' => $langs->trans("BoxLastExpiredServices",$max)); diff --git a/htdocs/core/lib/date.lib.php b/htdocs/core/lib/date.lib.php index 696934f4f85..eb20616f73f 100644 --- a/htdocs/core/lib/date.lib.php +++ b/htdocs/core/lib/date.lib.php @@ -67,27 +67,27 @@ function get_tz_array() * * @return string PHP server timezone string ('Europe/Paris') */ -function getCurrentTimeZoneString() +function getServerTimeZoneString() { if (function_exists('date_default_timezone_get')) return date_default_timezone_get(); else return ''; } - /** * Return server timezone int. * If $conf->global->MAIN_NEW_DATE is set, we use new behaviour: All convertions take care of dayling saving time. * - * @return string An offset in seconds (3600 for Europe/Paris on winter and 7200 for Europe/Paris on summer) + * @param string $refdate Reference date for timezone (timezone differs on winter and summer) + * @return int An offset in hour (+1 for Europe/Paris on winter and +2 for Europe/Paris on summer) */ -function getCurrentTimeZoneInt() +function getServerTimeZoneInt($refgmtdate='now') { global $conf; if (class_exists('DateTime') && ! empty($conf->global->MAIN_NEW_DATE)) { // Method 1 (include daylight) - $localtz = new DateTimeZone(date_default_timezone_get()); - $localdt = new DateTime("now", $localtz); + $localtz = new DateTimeZone(getServerTimeZoneString()); + $localdt = new DateTime($refgmtdate, $localtz); $tmp=-1*$localtz->getOffset($localdt); } else @@ -95,10 +95,48 @@ function getCurrentTimeZoneInt() // Method 2 (does not include daylight) $tmp=dol_mktime(0,0,0,1,1,1970); } - $tz=($tmp<0?'+':'-').abs($tmp/3600); + $tz=($tmp<0?1:-1)*abs($tmp/3600); return $tz; } +/** + * Return server timezone string + * + * @return string Parent company timezone string ('Europe/Paris') + */ +/*function getParentCompanyTimeZoneString() +{ + if (function_exists('date_default_timezone_get')) return date_default_timezone_get(); + else return ''; +} +*/ + +/** + * Return parent company timezone int. + * If $conf->global->MAIN_NEW_DATE is set, we use new behaviour: All convertions take care of dayling saving time. + * + * @param string $refdate Reference date for timezone (timezone differs on winter and summer) + * @return int An offset in hour (+1 for Europe/Paris on winter and +2 for Europe/Paris on summer) + */ +/*function getParentCompanyTimeZoneInt($refgmtdate='now') +{ + global $conf; + if (class_exists('DateTime') && ! empty($conf->global->MAIN_NEW_DATE)) + { + // Method 1 (include daylight) + $localtz = new DateTimeZone(getParentCompanyTimeZoneString()); + $localdt = new DateTime($refgmtdate, $localtz); + $tmp=-1*$localtz->getOffset($localdt); + } + else + { + // Method 2 (does not include daylight) + $tmp=dol_mktime(0,0,0,1,1,1970); + } + $tz=($tmp<0?1:-1)*abs($tmp/3600); + return $tz; +}*/ + /** * Add a delay of a timezone to a date diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 85fa19196b0..c587cecffa1 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -383,39 +383,6 @@ function dol_size($size,$type='') } -/** - * Return date for now. We should always use this function without parameters (that means GMT time) - * - * @param string $mode 'gmt' => we return GMT timestamp, - * 'tzserver' => we add the PHP server timezone - * 'tzref' => we add the company timezone - * 'tzuser' => we add the user timezone - * @return timestamp $date Timestamp - */ -function dol_now($mode='gmt') -{ - // Note that gmmktime and mktime return same value (GMT) whithout parameters - if ($mode == 'gmt') $ret=gmmktime(); // Time for now at greenwich. - else if ($mode == 'tzserver') // Time for now with PHP server timezone added - { - $tzsecond=-dol_mktime(0,0,0,1,1,1970); - $ret=gmmktime()+$tzsecond; - } - else if ($mode == 'tzref') // Time for now where parent company timezone is added - { - // TODO Should add the company timezone - $ret=gmmktime(); - } - else if ($mode == 'tzuser') // Time for now where user timezone is added - { - //print 'eeee'.time().'-'.mktime().'-'.gmmktime(); - $tzhour=isset($_SESSION['dol_tz'])?$_SESSION['dol_tz']:0; - $ret=gmmktime()+($tzhour*60*60); - } - return $ret; -} - - /** * Clean a string to use it as a file name * @@ -1094,6 +1061,42 @@ function dol_mktime($hour,$minute,$second,$month,$day,$year,$gm=false,$check=1) } +/** + * Return date for now. We should always use this function without parameters (that means GMT time) + * + * @param string $mode 'gmt' => we return GMT timestamp, + * 'tzserver' => we add the PHP server timezone + * 'tzref' => we add the company timezone + * 'tzuser' => we add the user timezone + * @return timestamp $date Timestamp + */ +function dol_now($mode='gmt') +{ + // Note that gmmktime and mktime return same value (GMT) whithout parameters + if ($mode == 'gmt') $ret=gmmktime(); // Time for now at greenwich. + else if ($mode == 'tzserver') // Time for now with PHP server timezone added + { + require_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'); + $tzsecond=getServerTimeZoneInt(); // Contains tz+dayling saving time + $ret=dol_now('gmt')+($tzsecond*3600); + } + /*else if ($mode == 'tzref') // Time for now with parent company timezone is added + { + require_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'); + $tzsecond=getParentCompanyTimeZoneInt(); // Contains tz+dayling saving time + $ret=dol_now('gmt')+($tzsecond*3600); + }*/ + else if ($mode == 'tzuser') // Time for now with user timezone is added + { + //print 'eeee'.time().'-'.mktime().'-'.gmmktime(); + $offsettz=(empty($_SESSION['dol_tz'])?0:$_SESSION['dol_tz'])*60*60; + $offsetdst=(empty($_SESSION['dol_dst'])?0:$_SESSION['dol_dst'])*60*60; + $ret=dol_now('gmt')+($offsettz+$offsetdst); + } + return $ret; +} + + /** * Return string with formated size * diff --git a/htdocs/includes/adodbtime/adodb-time.inc.php b/htdocs/includes/adodbtime/adodb-time.inc.php index 329579f6ed2..7e086300352 100644 --- a/htdocs/includes/adodbtime/adodb-time.inc.php +++ b/htdocs/includes/adodbtime/adodb-time.inc.php @@ -682,30 +682,6 @@ function _adodb_getdate($origd=false,$fast=false,$is_gmt=false) ); } -function adodb_gmdate($fmt,$d=false) -{ - return adodb_date($fmt,$d,true); -} - -// accepts unix timestamp and iso date format in $d -function adodb_date2($fmt, $d=false, $is_gmt=false) -{ - if ($d !== false) { - if (!preg_match( - "|^([0-9]{4})[-/\.]?([0-9]{1,2})[-/\.]?([0-9]{1,2})[ -]?(([0-9]{1,2}):?([0-9]{1,2}):?([0-9\.]{1,4}))?|", - ($d), $rr)) return adodb_date($fmt,false,$is_gmt); - - if ($rr[1] <= 100 && $rr[2]<= 1) return adodb_date($fmt,false,$is_gmt); - - // h-m-s-MM-DD-YY - if (!isset($rr[5])) $d = adodb_mktime(0,0,0,$rr[2],$rr[3],$rr[1]); - else $d = @adodb_mktime($rr[5],$rr[6],$rr[7],$rr[2],$rr[3],$rr[1]); - } - - return adodb_date($fmt,$d,$is_gmt); -} - - /** Return formatted date based on timestamp $d */ @@ -848,15 +824,6 @@ static $daylight; return $dates; } -/** - Returns a timestamp given a GMT/UTC time. - Note that $is_dst is not implemented and is ignored. -*/ -function adodb_gmmktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false) -{ - return adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,true); -} - /** Return a timestamp given a local time. Originally by jackbbs. Note that $is_dst is not implemented and is ignored. @@ -966,11 +933,6 @@ function adodb_mktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=fa return $ret; } -function adodb_gmstrftime($fmt, $ts=false) -{ - return adodb_strftime($fmt,$ts,true); -} - // hack - convert to adodb_date function adodb_strftime($fmt, $ts=false,$is_gmt=false) { diff --git a/test/phpunit/FunctionsTest.php b/test/phpunit/FunctionsTest.php index 0a98205f034..63a966d5811 100755 --- a/test/phpunit/FunctionsTest.php +++ b/test/phpunit/FunctionsTest.php @@ -254,10 +254,23 @@ class FunctionsTest extends PHPUnit_Framework_TestCase print __METHOD__." result=".$result."\n"; $this->assertEquals(7261,$result); - $tz=getCurrentTimeZoneInt(); - $result=dol_mktime(2,0,0,1,1,1970,0); // 1970-01-01 02:00:00 in local area + $result=dol_mktime(2,0,0,1,1,1970,0); // 1970-01-01 02:00:00 in local area Europe/Paris -> 3600 GMT print __METHOD__." result=".$result."\n"; - $this->assertEquals(7200-($tz*3600),$result); + $tz=getServerTimeZoneInt('1970-01-01 02:00:00'); // +1 in Europe/Paris at this time (we are winter) + $this->assertEquals(7200-($tz*3600),$result); // Should be 7200 if we are at greenwich + } + + /** + * testDolNow + * + * @return void + */ + public function testDolNow() + { + $now=dol_now('gmt'); + $nowtzserver=dol_now('tzserver'); + print __METHOD__."getServerTimeZoneInt=".(getServerTimeZoneInt()*3600)."\n"; + $this->assertEquals(getServerTimeZoneInt()*3600,($nowtzserver-$now)); } } ?> \ No newline at end of file