note 100357 modified in function.date-default-timezone-set by danbrown

From: Date: Mon, 11 Oct 2010 16:43:27 +0000
Subject: note 100357 modified in function.date-default-timezone-set by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173141@lists.php.net to get a copy of this message
Most of the scripts here depend on PHP identifying the correct zone when one is not set, but that does not always happen. For example PHP 5.3.3 gives a 'UTC' result for a detected '8.0/no DST'. Not much help. So for those of us who for very good reasons really cannot set time zones in scripts of in .ini files, here is a function for use in a Windows environment that sets the default time zone to the OS time zone and returns the detected and set texts. <?php function date_system_timezone_set(){ $shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host"); $time_bias = -($shell->RegRead( "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control \\TimeZoneInformation\\Bias"))/60; $ab = -($shell->RegRead( "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control \\TimeZoneInformation\\ActiveTimeBias"))/60; $sc = $shell->RegRead( "HKEY_USERS\\.DEFAULT\\Control Panel\\International\\sCountry"); foreach(timezone_abbreviations_list() as $tza) foreach($tza as $entry){ $country = strtok($entry['timezone_id'],'/'); $locale = strtok('|'); if($country==$sc && $ab==($entry['offset']/60/60) && ($ds = $time_bias!=$ab)==$entry['dst']){ date_default_timezone_set($timezone_identifier = $country."/".$locale); return sprintf('%.1f',$ab)."/".($ds?'':'no ').'DST'." ".$timezone_identifier; } } return false; } ?> --was-- Most of the scripts here depend on PHP identifying the correct zone when one is not set, but that does not always happen. For example PHP 5.3.3 gives a 'UTC' result for a detected '8.0/no DST'. Not much help. So for those of us who for very good reasons really cannot set time zones in scripts of in .ini files, here is a function for use in a Windows environment that sets the default time zone to the OS time zone and returns the detected and set texts. function date_system_timezone_set(){ $shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host"); $time_bias = -($shell->RegRead( "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control \\TimeZoneInformation\\Bias"))/60; $ab = -($shell->RegRead( "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control \\TimeZoneInformation\\ActiveTimeBias"))/60; $sc = $shell->RegRead( "HKEY_USERS\\.DEFAULT\\Control Panel\\International\\sCountry"); foreach(timezone_abbreviations_list() as $tza) foreach($tza as $entry){ $country = strtok($entry['timezone_id'],'/'); $locale = strtok('|'); if($country==$sc && $ab==($entry['offset']/60/60) && ($ds = $time_bias!=$ab)==$entry['dst']){ date_default_timezone_set($timezone_identifier = $country."/".$locale); return sprintf('%.1f',$ab)."/".($ds?'':'no ').'DST'." ".$timezone_identifier; } } return false; } http://php.net/manual/en/function.date-default-timezone-set.php

« previous php.notes (#173141) next »