6

I am using this code to minus 48 hours from a timestamp

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

This works fine, i want to add 48 hours, i have tried:

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

I have echoed $result2["datetime"]; which shows a timestamp in the format Y-m-d H:i:s

and when i use:

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

that adds the 48 hours on fine too

When i use

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

Its just echoing the same timestamp thats returned from $result2["datetime"]; and not +48 hours

4

1 回答 1

11

第一个参数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');

演示

于 2014-10-21T19:59:42.350 回答