PHP时区超前或滞后


PHP timezone ahead or behind?

我在谷歌上搜索过,读过一些关于时区和偏移量的PHP手册,但似乎找不到这个问题的答案!

所以我要在这里问它。如果这是一个重复的问题或有其他答案,请让我知道,我会删除它。

目前,我正在使用下面的代码来显示PHP中两个位置之间的时间差。

代码运行良好,但存在问题。如果2位置第一";在";位置二";,结果将在数字前面加一个减号。

例如:如果第一个地点是纽约,第二个地点是伦敦,则时差将显示为-5。

如果是相反的情况,即伦敦第一,纽约第二,则显示为5。

我需要做的是显示这样的时差:

伦敦5小时;前方";纽约。

纽约是5小时";在"后面";伦敦。

这是代码:

<?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);
}
?>

在这里我回应了结果:

<?php echo $offset/3600; ?>

这是位置的HTML下拉列表:

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

顺便说一下,时区1和时区2下拉列表是相同的。

有人能帮我一下吗?

这实际上是一个简单逻辑的问题。使用if()语句,并根据偏移量值指定要回显的适当文本。

$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}";

理念

使用可以使用您的符号$offset来确定要显示的字符串:

  • 正面:A领先于B
  • 否定:A在B后面
  • 零:相同

代码

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

参考文献

  • gmp_sign

如果你想更改结果的符号,你可以这样做:<?php echo $offset/3600*-1; ?>

如果您只想删除减号<?php echo ($offset < 0) ? $offset/3600*-1 : $offset; ?>

如果你要去掉写句子的符号,你可以这样做:

<?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]);
?>

我认为这应该与您在问题中提供的代码配合使用。

格林尼治标准时间
如果你使用time(),它是以GMT为单位的,因为它是自Unix大纪元(1970年1月1日00:00:00 GMT)以来一直在计算秒数的Unix时间戳。