使用 date() 函数在特定时区显示时间/日期

php
2022-08-30 21:43:40

我使用 date() 函数来获取日、月和年。

$year = date(y);
$month  = date(m);
$day = date(d);

但是我的托管在我所在的另一个地方,所以我需要增加11个小时。你能告诉我我该怎么做吗?谢谢


答案 1

要么做

date('Y-m-d', strtotime('+11 hours'));

以添加 11 小时或创建 DateTime 对象并在需要时更改其时区

$datetime = new DateTime; // current time = server time
$otherTZ  = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($otherTZ); // calculates with new TZ now

或者只需设置适当的时区


答案 2

您可以使用 http://www.php.net/manual/en/function.date-default-timezone-set.php 将时区设置为所需的时区


推荐