douding7189
2013-10-27 10:02 阅读 32
已采纳

在php中设置时区

I'm using the following code to get server time. But I'm in Bangladesh and I don't get Bangladeshi time by this. Please tell me where I have to change for the exact Bangladeshi time.

$Vdate=date("F j, Y, g:i a");
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • 已采纳
    douzhi3779 douzhi3779 2013-10-27 11:29

    Function date_default_timezone_set()>= 5.1.0 set timezone globally.

    If you need to set timezone locally, for specific variable, you can use DateTime>= 5.2.0 and DateTimezone>= 5.2.0 classes, like:

    $dt = new DateTime('now', new DateTimezone('Asia/Dhaka'));
    echo $dt->format('F j, Y, g:i a');
    

    Here is the list of all available timezones in PHP.


    Since non of the above functions will work on PHP version 4.x, you have no other way to set timezone, rather that setting your server time to your timezone, or add offset to time() functions, like:

    echo date('F j, Y, g:i a', time() - 6*3600); # Bangladesh is in UTC+6
    
    点赞 评论 复制链接分享
  • dqm83011 dqm83011 2013-10-27 10:07
    点赞 评论 复制链接分享
  • doudaifu6083 doudaifu6083 2013-10-27 10:07

    You have to use:

    date_default_timezone_set('Asia/Dhaka');
    

    I'm not sure if this is the right timezone.

    点赞 评论 复制链接分享
  • dongzhuanlei0768 dongzhuanlei0768 2013-10-27 10:09
    date_default_timezone_set('Asia/Dhaka');
    

    add this line before or set it in your php.ini file

    点赞 评论 复制链接分享
  • dpdrtj1075 dpdrtj1075 2013-10-28 09:22

    I think the proper way to set your timezone is to set it in your php.ini configuration file. You just have to set date.timezone = "[Valid timezone value]".

    点赞 评论 复制链接分享
  • douza1373 douza1373 2018-12-18 06:31
    <?php
    date_default_timezone_set("Asia/Dhaka");
    echo "The time is " . date("Y-m-d h:i:sa");
    ?>
    
    点赞 评论 复制链接分享

相关推荐