PHP时区前后?
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
It is actually a problem of simple logic. Use an if() statement and specify the appropriate text to be echoed based on the offset value.
$str = '';
if (0 > $offset)
{
// For negative offset (hours behind)
$hour_diff = $offset / 3600 * -1;
$str = "{$hour_diff} hour(s) behind of";
}
elseif (0 < $offset)
{
// For positive offset (hours ahead)
$hour_diff = $offset / 3600;
$str = "{$hour_diff} hour(s) ahead of";
}
else
{
// Have you considered the case of same timezone?
$str = "in the same timezone as";
}
echo "{$location1} is {$str} {$location2}";
Greenwich Mean Time
Well if your using time(), it's in GMT as it is the Unix timestamp that's been counting seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Hope that helps :)
Idea
Use can use the sign of you $offset
to determine the string to display:
- Positive: A ahead of B ;
- Negative: A behind B ;
- Zero: same.
Code
function tzPosition( $number ) {
return ( $number > 0 ) ? 'ahead' : ( ( $number < 0 ) ? 'behind' : 'same' );
}
echo sprintf("%s is %s hours %s of %s", $cityA, abs($offset), tzPosition($offset), $cityB);
References
If you want to change the sign of the result, you can do this: <?php echo $offset/3600*-1; ?>
If you only want to remove the minus <?php echo ($offset < 0) ? $offset/3600*-1 : $offset; ?>
If you are going to remove the sign to write the sentence you can do this:
<?php
//check direction
$direction = ($offset < 0)? "ahead" : "behind";
$direction = ($offset === 0)? "same timezone as" : $direction;
//remove the -
$hours = $offset/3600;
$hours = ($offset < 0) ? $hours*-1 : $hours;
//Get the cities from the Timezone
preg_match('#.*/([^/]+$)#',$timezone1,$city1);
preg_match('#.*/([^/]+$)#',$timezone2,$city2);
//echo sentence replacing underscores with spaces.
echo str_replace("_", " ", $city2[1]).' is '.$hours.' hours '.$direction.' '.str_replace("_", " ", $city1[1]);
?>
I think this should work with the code you provided in the question.