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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2607545/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:05:54  来源:igfitidea点击:

Display time/date in specific timezone using date() function

php

提问by Simon

I use the date() function to get day, month and year.

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

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

But my hosting is in another place where I am, so I need to add 11 hours. could you tell me how can I do that? thanks

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

回答by Gordon

Either do

要么做

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

to add 11 hours or create a DateTime object and change it's timezone where needed

添加 11 小时或创建一个 DateTime 对象并在需要时更改它的时区

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

or simply set the appropriate timezone with

或者简单地设置适当的时区

回答by chris

you could use http://www.php.net/manual/en/function.date-default-timezone-set.phpto set the timezone to what you want

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