简体   繁体   English

如何在php中获取本地日期和时间

[英]how to get local date and time in php

我想用php计算我的国家((新西兰)本地日期和时间,但我不知道如何计算。请任何人都可以帮助我。谢谢

PHP >=5.2.0 PHP> = 5.2.0

$nz_time = new DateTime(null, new DateTimezone('Pacific/Auckland'));
echo $nz_time->format('Y-m-d H:i:s');

PHP 5 PHP 5

date_default_timezone_set('Pacific/Auckland');
echo date('Y-m-d H:i:s');

http://us3.php.net/manual/en/function.date.php http://us3.php.net/manual/zh/function.date.php

If you are using PHP 5.1.0 or later you can also use the timestamp modifier to get the specific time you need. 如果您使用的是PHP 5.1.0或更高版本,则还可以使用timestamp修饰符来获取所需的特定时间。

Otherwise you just need to make the proper calculations to the local time you need against the server time. 否则,您只需要针对所需的本地时间与服务器时间进行正确的计算即可。

您必须设置默认时区,然后date函数将返回您当前的日期和时间date_default_timezone_set

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

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