I have searched on Google and read some of the PHP manuals on timezone and offsets but seem can't find the answer to this question!
so i am going to ask it here. please let me know if this is a duplicated question or has an answer else where and i will delete it.
currently I am using the code bellow to show the time difference between two locations in PHP.
the code works fine But there is an issue. if the 2location first" is behind the "location two", the result will be shown with a minus before the number.
for example: if the first location is New York and the second one is London the time difference will be shown as -5.
and if it was the other way round, i.e. London first and the New York second it will be shown as 5.
what i need to do is to show the time difference like this:
london is 5 hours "ahead" of New York.
or
New York is 5 hours "behind" of london.
here is my code:
<?php
if( isset($_POST['submit']))
{
//be sure to validate and clean your variables
$timezone1 = htmlentities($_POST['timezone1']);
$timezone2 = htmlentities($_POST['timezone2']);
//then you can use them in a PHP function.
function get_timezone_offset( $origin_tz, $remote_tz ) {
$timezone1 = new DateTimeZone( $origin_tz );
$timezone2 = new DateTimeZone( $remote_tz );
$datetime1 = new DateTime("now", $timezone1);
$datetime2 = new DateTime("now", $timezone2);
$offset = $timezone1->getOffset($datetime1) - $timezone2->getOffset($datetime2);
return $offset;
}
$offset = get_timezone_offset($timezone1, $timezone2);
}
?>
and here i echo the result:
<?php echo $offset/3600; ?>
and this the HTML dropdown list of locations:
<form id="myForm" name="myForm" class="myForm" method="post" action="">
<select name="timezone2" id="timezone2" class="timezone2">
<optgroup label="Africa">
<?php
foreach($options as $key => $value)
{
echo '<option value="'. $key .'" label="'. $value .'">'.$value.'</option>';
}
?>
<option value="Africa/Abidjan" label="Abidjan">Abidjan</option>
<option value="Africa/Accra" label="Accra">Accra</option>
<option value="Africa/Addis_Ababa" label="Addis Ababa">Addis Ababa</option>
<option value="Africa/Algiers" label="Algiers">Algiers</option>
<option value="Africa/Asmara" label="Asmara">Asmara</option>
</optgroup>
</select>
the timezone1 and timezone2 dropdownlists are identical by the way.
could someone help me out with this?
Thanks