php 在php中设置时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19616822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting time zone in php
提问by Mak Reza
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");
回答by Glavi?
Function date_default_timezone_set()
>= 5.1.0set timezone globally.
功能>= 5.1.0全局设置时区。date_default_timezone_set()
If you need to set timezone locally, for specific variable, you can use DateTime>= 5.2.0and DateTimezone>= 5.2.0classes, like:
如果您需要在本地设置时区,对于特定变量,您可以使用DateTime >= 5.2.0和DateTimezone >= 5.2.0类,例如:
$dt = new DateTime('now', new DateTimezone('Asia/Dhaka'));
echo $dt->format('F j, Y, g:i a');
Here is the list of all available timezonesin 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:
由于上述函数都不适用于 PHP 4.x 版,因此您没有其他方法可以设置时区,而是将服务器时间设置为您的时区,或者为time()
函数添加偏移量,例如:
echo date('F j, Y, g:i a', time() - 6*3600); # Bangladesh is in UTC+6
回答by Alex
You have to use:
你必须使用:
date_default_timezone_set('Asia/Dhaka');
I'm not sure if this is the right timezone.
我不确定这是否是正确的时区。
回答by rfoo
Check out date_default_timezone_set()
查看 date_default_timezone_set()
http://php.net/manual/en/function.date-default-timezone-set.php
http://php.net/manual/en/function.date-default-timezone-set.php
回答by steven_ght
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]"
.
我认为设置时区的正确方法是在 php.ini 配置文件中进行设置。你只需要设置.date.timezone = "[Valid timezone value]"
回答by nafischonchol
<?php
date_default_timezone_set("Asia/Dhaka");
echo "The time is " . date("Y-m-d h:i:sa");
?>
回答by user12379030
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Himel Sarkar</title>
<link rel="stylesheet" href="">
</head>
<body>
<?php
date_default_timezone_set('Asia/Dhaka');
echo date("d-M-y h:i") ;
?>
</body>
</html>
回答by Adassko
date_default_timezone_set('Asia/Dhaka');
add this line before or set it in your php.ini
file
在您的php.ini
文件之前添加此行或将其设置