PHP 设置时区 [英] Php Set TimeZone
问题描述
我有以下代码:
$date = new DateTime(date("Y-m-d H:i:s"), new DateTimeZone('Asia/Karachi'));
echo $date->format('Y-m-d H:i:s');
输出:2015-08-26 17:46:05
实际结果应该是:2015-08-26 13:46:05
如何设置时区,让实际输出来?
How to set the time zone, so that actual output comes?
推荐答案
方法一(编辑Php.ini)
1) 打开你的 php.ini 文件
1) Open your php.ini file
2) 将以下代码行添加到 php.ini 文件的顶部
2) Add the following line of code to top of your php.ini file
date.timezone = "Asia/Karachi"
3) 重启 php.
注意:您可以在此处找到时区列表:http://php.net/manual/en/timezones.asia.php
Note: You can find a list of timezone here: http://php.net/manual/en/timezones.asia.php
方法 2(使用 date_default_timezone_set)
如果您无权访问 php.ini
文件,您可以使用 date_default_timezone_set("Asia/Karachi")
.只需将其放在应用程序的入口点即可.
In case you don't have access to the php.ini
file, you can use date_default_timezone_set("Asia/Karachi")
. Just put it in the entry point of your application.
http://php.net/manual/en/function.date-default-timezone-set.php
这篇关于PHP 设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!