pandu Posted July 12, 2010 Share Posted July 12, 2010 Hi, I am studying from a oreilly book and according to it, the following code should be converting time from one timezone to another. But when I run it on my computer - the time in one zone is not converted to another. It just shows that 10:06pm in East time zone is 10:06pm in Pacific time zone. I have gone thru the code several times, and the only reason I can think of is that something has changed with the way the date function generates time. I am guessing it does not use the TZ environment variable in PHP 5. Is that why this code is not working? Note - I am not getting any error, its just not converting the time. <html> <head> <TITLE>time converter</title> </head> <body> <?php error_reporting (E_ALL ^ E_NOTICE); ?> <?php $time_zones = array("Asia/Hong Kong","Africa/Cairo","Europe/Paris","Europe/Madrid","Europe/London","Asia/Tokyo","North_America/New_York","North_America/Los_Angeles","North_America/Chicago"); if ($_GET["start_time"] != NULL){ $start_time_input = $_GET["start_time"]; $start_tz = $_GET["start_tz"]; $end_tz = $_GET["end_tz"]; putenv("TZ=$start_tz"); $start_time = strtotime($start_time_input); echo "<p><strong>"; echo date("h:i:sA",$start_time)."\n"; echo "</strong>"; putenv("TZ=$end_tz"); echo "in $start_tz becomes "; echo "<strong> "; echo date("h:i:sA",$start_time)."\n"; echo "</strong>"; echo " in $end_tz.</p><hr />"; } ?> <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET"> <label> Your Time: <input type="text" name="start_time" value="<?php echo $start_time_input; ?>" /> </label> in <select name="start_tz"> <?php foreach ($time_zones as $tz) { echo '<option'; if (strcmp($tz, $start_tz) == 0){ echo ' selected="selected"'; } echo ">$tz</option>"; } ?> </select> <p>Convert to: <select name="end_tz"> <?php foreach ($time_zones as $tz) { echo '<option'; if (strcmp($tz, $end_tz) == 0) { echo ' selected="selected"'; } echo ">$tz</option>"; } ?> </select> <input type="submit" value="Convert!"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/207464-date-code-not-working/ Share on other sites More sharing options...
kenrbnsn Posted July 12, 2010 Share Posted July 12, 2010 Where are you running the code? On a Linux server or on Windows? Ken Quote Link to comment https://forums.phpfreaks.com/topic/207464-date-code-not-working/#findComment-1084670 Share on other sites More sharing options...
pandu Posted July 12, 2010 Author Share Posted July 12, 2010 Running on windows using XAMPP Quote Link to comment https://forums.phpfreaks.com/topic/207464-date-code-not-working/#findComment-1084671 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2010 Share Posted July 12, 2010 You need to use - date_default_timezone_set in your script to set the timezone in your script. I could not get setting the TZ env variable in a script to override the date.timezone setting despite what the php.net documentation states. The TZ env variable is being set and read back but has no affect on the timezone your script uses (it is likely only read once at the start of execution.) Use the following instead of the putenv() statements - date_default_timezone_set($start_tz); date_default_timezone_set($end_tz); Also, all of the North_America/... settings are invalid. They should just be America/... (without the North_ ) Quote Link to comment https://forums.phpfreaks.com/topic/207464-date-code-not-working/#findComment-1084715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.