strtotime, strcmp, and Time Zones question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHP_Wanabe
Forum Newbie
Posts: 7
Joined: Tue Apr 13, 2010 10:55 pm

strtotime, strcmp, and Time Zones question

Post by PHP_Wanabe »

I have another problem, this time I am following an example tutorial that creates a form in which a user is able to select a time zone and convert the time the desired time into the time at on that time zone.

I had a problem with undefined warning messages but that is solved now.

The only problem I have is that once I input the time it does not convert it into the time on the selected time zone.

When I click the button to process the form it displays the results page but the time does not change.

The time is the same on the current time zone and on the selected time zone.

This is the form:

Code: Select all

<html>
<head>
<title>Time Zone Converter</title>
</head><body>
<?php
// An array holds the standard time zone strings
$time_zones = array(
"Asia/Hong_Kong",
"Africa/Cairo",
"Europe/Paris",
"Europe/Madrid",
"Europe/London",
"Asia/Tokyo",
"America/New_York",
"America/Los_Angeles",
"America/Chicago");
// Check to see if the form has been submitted
if (isset($_GET['start_time'])) {
$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 />";
} else {
$start_time_input = '';
$start_tz = '';
$end_tz = '';
}
?>
<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>

I would appreciate any help in guiding me to the best way to figure out how to fix this problem.


It seems that the book I trusted will give some tools to learn PHP is not that good (Learning PHP and MySQl, Oreilly 1st Ed). So, if anybody has an suggestion on a great book for beginners PHP programmers I'll appreciate a title or two.

Thanks,
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: strtotime, strcmp, and Time Zones question

Post by Technocrat »

I dont see where you are setting the date_default_timezone_set
PHP_Wanabe
Forum Newbie
Posts: 7
Joined: Tue Apr 13, 2010 10:55 pm

Re: strtotime, strcmp, and Time Zones question

Post by PHP_Wanabe »

I added the following statement after line 5

Code: Select all

date_default_timezone_set('America/Los_Angeles');
there is no change in the behavior of the form after its submitted, it still shows me the same time that is inputed as the same time is supposed to be converted into.

I uploaded the form to my website if you want to take a loot at what I get. This is the link: http://videoszacatecas.com/example3.php

Thanks for your continued help,
Post Reply