PHP制作时间变量?5表示一小时,然后回显


PHP Making Time variables? 5 for the hour and then echo it back?

只是一个简单的问题,我如何能够将以下日期/时间变量编辑为加上(+)小时数5,目前是美国时间,我需要将所有内容转换为英国时间。

$tme = date('H:i : d F Y');

有什么想法吗。。。?

使用DateTime对象,它更健壮,而且不会让您头疼:

$time = new DateTime("now", new DateTimeZone("America/New_York"));
$time->setTimezone(new DateTimeZone("Europe/London"));
echo $time->format("H:i:s Y-m-d");

还要注意的是,有了这个,你可以在不做任何计算的情况下处理与时区相关的奇怪的事情(比如一些时区在不同的日期变化,有些取决于年份等),DateTimeZone对象为你做这件事:-)

将其添加到页面顶部:

date_default_timezone_set('Europe/London');

使用date_default_timezone_set函数

date_default_timezone_set('Europe/London');
$tme = date('H:i : d F Y', time() + 60*5);

但是您可能对DateTime对象感兴趣

或使用更改时区

date_default_timezone_set('Europe/London');

改用DateTimeDateTimeZone

// U.S. timezone (EST)
$tme = new DateTime('now', new DateTimeZone('America/New_York'));
echo $tme->format('H:i : d F Y');
// U.K. timezone
$tme->setTimeZone(new DateTimeZone('Europe/London'));
echo $tme->format('H:i : d F Y');