Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/207464-date-code-not-working/
Share on other sites

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_ )

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.