|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-07-04 00:32 UTC] easysurf at easysurf dot cc
Description: ------------ Africa/Cairo timezone is off by one hour. Expected result: ---------------- If it is 10:00 am in Cairo it should not say 11:00 PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 22:00:01 2025 UTC |
<?php function recity($anycity) { $lcity1 = strlen($anycity); $pos1 = strpos($anycity, "/"); $pos1 = $pos1 + 1; $sstrt1 = $lcity1 - $pos1; $tncity = substr($anycity, $pos1, $sstrt1); $tncity2 = ""; for ($i = 0, $j = strlen($tncity); $i <= $j; $i++) { if (substr($tncity, $i, 1) == "_") { $tncity2 = $tncity2 . " "; } else { $tncity2 = $tncity2 . substr($tncity, $i, 1); } } return $tncity2; } $time1 = date("g:i A", mktime()); $city1 = "GMT"; $city2 = "Africa/Cairo"; $ncity1 = recity($city1); $ncity2 = recity($city2); $start_time_input = $time1; $start_tz = $city1; $end_tz = $city2; putenv("TZ=$start_tz"); $start_time = strtotime($start_time_input); echo "<p><strong>"; echo "<BR /><br />"; echo "<BLOCKQUOTE><FONT SIZE=6>"; putenv("TZ=$end_tz"); echo date("l g:i A",$start_time)."\n"; echo "</strong>"; echo " in $ncity2.</p>"; echo "</FONT></BLOCKQUOTE>"; echo "<BR /><br />"; echo "<P>return to <A HREF=http://www.easysurf.cc/tmnw4.htm>What Time is it now?</A><P>"; ?>I'm not sure what you're trying to do in that script, but i suppose it's a bug in it. You convert from local to GMT and then again. I cannot repro neither with your script nor with a much smaller sample putenv('TZ=Africa/Cairo'); echo date('l g:i A'); Do that two lines work for you?<?php putenv('TZ=Africa/Cairo'); echo date('l g:i A'); ?> The problem is that your site that sends out the time zone for Africa/Cairo is off by an hour. When I tried the above code it is still one hour off. Goto: http://wwp.greenwichmeantime.com/time-zone/africa/egypt/cairo/ You will see that there is a discrepancy between your time and theirs. Also when I watch CNN with a live feed from Cairo with a timestamp, it does not match you time but does match the time that Greenwich sends out.Windows/PHP 5.3.26 CLI: ab's code: - putenv() not respected, outputs default (local) time with timezone warnings (getenv() says Cairo, $_ENV says nothing) - manually setting it with "set TZ=Africa/Cairo" on the command line before executing works (getenv() and $_ENV say Cairo) - date_default_timezone_set() instead works easysurf's code: - outputs default (local) time with timezone warnings Ubuntu/PHP 5.3.10: ab's code: - putenv() works - date_default_timezone_set() works easysurf's code: - outputs local+2h with timezone warnings The code does have a bug as ab saw: 1. Grabs the current time as a string according to the current timezone 2. Interprets the string according to the GMT timezone and gets a timestamp 3. Uses the timestamp to output the time in Cairo Net effect is adding 2 hours (Cairo minus GMT) to the current time. Fix: don't do anything at all with GMT and use date_default_timezone_set() instead of putenv(TZ). Like literally <?php date_default_timezone_set($city2); echo date("l g:i A")."\n"; ?> All that aside, 5.3 is now EOL and 5.4+ doesn't use TZ.