duangewu5234 2013-08-27 15:44
浏览 25
已采纳

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

  • 写回答

4条回答 默认 最新

  • douxiapi4381 2013-08-27 15:58
    关注

    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}";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多