在php中从日期中减去几个小时


subtract a few hours from a date in php

我正在尝试使用以下命令回显文件的时间戳:

Last updated: <?= date("m/d/Y H:i:s",filemtime("file1.html")) ?>

但是这是在一个提前6小时的服务器上。

我已经尝试使用DateTime::sub或date_sub或sub,但这些都没有被识别。我需要调用date类吗?

filemtime返回时间戳,只需减去所需的秒数。

date("m/d/Y H:i:s",filemtime("file1.html")-6*3600)

或指定时区

$time = new Datetime(new Datetimezone('America/Chicago'));
$time->setTimestamp(filetime("file1.html"));
echo $time->format('m/d/Y H:i:s');