PHP Decrease a Few Seconds of the "datetime"

时间:2018-09-01 22:53:36

标签: php

In my code, I get the current time and date value of the server following this organization parameter:

Y-m-d H:i:s        Ex: 2018-09-01 22:35:18

I would like to remove 30 seconds of the time to use in the if comparison.

How can I accomplish this task?

2 个答案:

答案 0 :(得分:0)

使用 strtotime 函数来增加或减少时间。

如果要将当前时间戳减去30秒:

$thirtySecondsAgo = date('Y-m-d H:i:s', strtotime('-30seconds'));

如果参考日期不是现在:

$date = "2018-08-15 22:51:47";
$dateThirtySecondsAgo = date('Y-m-d H:i:s', strtotime($date . '-30seconds'));

答案 1 :(得分:0)

尝试以下操作,例如当前日期减去30秒:

$date = date('Y-m-d H:i:s', strtotime('-30 second'));