简体   繁体   English

如何在PHP中为不同时区设置时间设置?

[英]How to set time settings for different timezones in PHP?

I made a project with PHP that was expected to run inside my country Iran that its timezone is UTC+3:30. 我使用PHP进行了一个项目,该项目预计将在我的国家伊朗境内运行,其时区为UTC + 3:30。 I inserted the line below inside my codes: 我插入下面我里面的代码行:

date_default_timezone_set('Asia/Tehran');

But now, I think it may be used by others from different countries. 但是现在,我认为不同国家的其他人可能会使用它。 As a part if this project contains users signin logs, I must set the time matual with their country timezones. 作为此项目的一部分,如果该项目包含用户登录日志,则必须将时间与他们所在的国家/地区时区一起设置。
How can I do it? 我该怎么做?

Here is how you can set the timezone: 这是设置时区的方法:

<?php
session_start();
if(!isset($_SESSION['timezone']))
{
    if(!isset($_REQUEST['offset']))
    {
    ?>
    <script>
    var d = new Date()
    var offset= -d.getTimezoneOffset()/60;
    location.href = "<?php echo $_SERVER['PHP_SELF']; ?>?offset="+offset;
    </script>
    <?php    
    }
    else
    {
        $zonelist = array('Kwajalein' => -12.00, 'Pacific/Midway' => -11.00, 'Pacific/Honolulu' => -10.00, 'America/Anchorage' => -9.00, 'America/Los_Angeles' => -8.00, 'America/Denver' => -7.00, 'America/Tegucigalpa' => -6.00, 'America/New_York' => -5.00, 'America/Caracas' => -4.30, 'America/Halifax' => -4.00, 'America/St_Johns' => -3.30, 'America/Argentina/Buenos_Aires' => -3.00, 'America/Sao_Paulo' => -3.00, 'Atlantic/South_Georgia' => -2.00, 'Atlantic/Azores' => -1.00, 'Europe/Dublin' => 0, 'Europe/Belgrade' => 1.00, 'Europe/Minsk' => 2.00, 'Asia/Kuwait' => 3.00, 'Asia/Tehran' => 3.30, 'Asia/Muscat' => 4.00, 'Asia/Yekaterinburg' => 5.00, 'Asia/Kolkata' => 5.30, 'Asia/Katmandu' => 5.45, 'Asia/Dhaka' => 6.00, 'Asia/Rangoon' => 6.30, 'Asia/Krasnoyarsk' => 7.00, 'Asia/Brunei' => 8.00, 'Asia/Seoul' => 9.00, 'Australia/Darwin' => 9.30, 'Australia/Canberra' => 10.00, 'Asia/Magadan' => 11.00, 'Pacific/Fiji' => 12.00, 'Pacific/Tongatapu' => 13.00);
        $index = array_keys($zonelist, $_REQUEST['offset']);
        $_SESSION['timezone'] = $index[0];
    }
}
date_default_timezone_set($_SESSION['timezone']);

//rest of your code goes here
?>

Source 资源

But I recommend not to do that. 但我建议不要这样做。 It would be better to set GMT (or your timezone) as default and change the information from the logs according the current timezone of each user when it is displayed. 最好将GMT(或您的时区)设置为默认值,并根据显示每个用户的当前时区从日志中更改信息。

Try this: 尝试这个:

<?php
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
echo $fmt->format(time());
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM