your fault shall be forgiven :D
Seriously, I got your code working take a look at the result here: http://sofia.bplaced.net/homework2.php
It wasn't THAT wrong.
I don't now how reliable this page is, but this shows every change i made: http://www.diffchecker.com/cfdm0ppc
The new working code:
<?php
$timezones = array(
'EST' => -5*3600, //equador
'UTC' => 0, //london
'CCT' => +8*3600, //beijing
'PST' => -8*3600, //los angeles
);
foreach ($timezones as $time) {
$now = time();
$now += $time;
print gmstrftime('DATE: %B %d %Y and TIME: %r <br/>',$now);
}
$nowM = date('m');
$nowD = date('d');
$nowY = date('Y');
$nowHr = date('h');
$nowMin = date('i');
$nowSec = date('s');
?>
<form action="homework2.php" method="post">
<label>Sync Timecode</label>
<input type="text" name="month" placeholder="Month <?php $nowM ?>" value="<?php $nowM ?>" />
<input type="text" name="day" placeholder="Day <?php $nowD ?>" value="<?php $nowD ?>" />
<input type="text" name="year" placeholder="Year <?php $nowY ?>" value="<?php $nowY ?>" />
<input type="text" name="hour" placeholder="Hours <?php $nowHr ?>" value="<?php $nowHr ?>" />
<input type="text" name="min" placeholder="Minutes <?php $nowMin ?>" value="<?php $nowMin ?>"/>
<input type="text" name="sec" placeholder="Seconds<?php $nowSec ?>" value="<?php $nowSec ?>"/>
<input type="submit" id="button">
</form>
<?php
$format = 'd-m-Y H:i:s';
$queryTime = $_POST['day'] . "-" . date('m',strtotime($_POST['month'])) . "-" . $_POST['year'] . " " . $_POST['hour'] . ":" . $_POST['min'] . ":" . $_POST['sec'];
if (date($format,strtotime($queryTime)) == $queryTime) {
try {
$now = new DateTime(date("c",strtotime($queryTime)));
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
} else {
echo "You entered: " . $queryTime . "<hr style='display:block;'>";
exit;
}
$timezones = array(
'EST' => 'America/Panama',
'UTC' => 'Europe/London',
'CCT' => 'Asia/Hong_Kong',
'PST' => 'America/Los_Angeles',
);
echo "You entered: " . $now->format($format) . "<hr style='display:block;'>";
foreach ($timezones as $zone) {
$now = new DateTime(date("c",strtotime($queryTime)));
date_timezone_set($now, timezone_open($zone));
echo "The time in " . $zone . " is " . $now->format($format." \G\M\TP") . "<br/>";
}
?>
I had to alter the following things:
Your IF-statement was wrong, you compared a 12h Value to a 24H value line 35:
"h" to "H" //altered hour-format
I expected you wanted your user to enter a month like "April" and not like "04" line 36:
"$_POST['month']" to "date('m',strtotime($_POST['month']))" //altered month-format
You should always fetch error-exeptions - this way your code may go on in case of an error line 36:
altered to "try{...}"
line 41:
"You entered: " //added a spacer
line 45 ot 50:
modified the array as you tried to print these in line 56 and used them as parameter in 55
line 52:
"You entered: " //added a spacer
line 55:
"DateTime()" to "date_timezone_set()" //altered the command. The new cmd is part of the same function-set
line 56:
"print" to "echo" //I like uniform code
line 56(2 and 3):
"The time in " and "is " //added spacers
line 56(4):
" \G\M\TP" //relative time to GMT
you wrote something about "...display the time for each of the 4 times relative to the time that was manually entered." "\G\M\T" will print as "GMT" "P" will result in the time relative to GMT$
This should work for you.
Aiyion.Prime