如何在时间戳中添加48小时


how to add 48 hours to a timestamp

我使用此代码从时间戳减去48小时

echo date($result2["datetime"], strtotime("-48 hours"));

这工作得很好,我想添加48小时,我已经尝试过:

echo date($result2["datetime"], strtotime("+48 hours"));

我已经回复了$result2["datetime"];,它显示了格式为Y-m-d H:i:s 的时间戳

当我使用时:

echo date('Y-m-d H:i:s', strtotime("+48 hours"));

这增加了48小时的罚款太

当我使用时

echo date($result2["datetime"], strtotime("+48 hours"));

它只是呼应了从$result2["datetime"];返回的相同时间戳,而不是+48小时

date()的第一个参数是您希望日期输出的格式。第二个参数是要格式化的值。在那里,您将使用strtotime()来添加您的48小时。

echo date('Y-m-d H:i:s', strtotime($result2["datetime"] . " +48 hours"));

演示

或:

echo date('Y-m-d H:i:s', strtotime("+48 hours", strtotime($result2["datetime"])));

演示

不过,这有点难看。我建议使用DateTime()

echo (new DateTime($result2["datetime"]))->modify('+48 hours')->format('Y-m-d H:i:s');

演示