Php mysql时间戳 - 2小时

时间:2013-12-19 15:53:00

标签: php time

我现在用PHP工作了这个时间:

$today = date("Y-m-d H:i:s");

我无法弄清楚如何以相同的格式追加当前时间-2小时。 有人能帮我快点吗? 谢谢!

3 个答案:

答案 0 :(得分:0)

你在这里:

$time = time();
$prevtime = strtotime("-2 hours"); // time -2 hours
$date = data("Y-m-d H:i:s", $prevtime); // time -2 hours

希望它有所帮助;)

链接: PHP.net strtotime()

答案 1 :(得分:0)

您可以使用strtotime()

    $minus_two_hrs = date("Y-m-d H:i:s", strtotime("-2 hours"));

strtotime()函数返回一个时间戳,然后您可以将其传递给date()以获取格式正确的日期。

答案 2 :(得分:0)

您也可以手动操作unix时间戳;因为时间戳以秒为单位:

e.g。

// Get current time in seconds
$time_now = time();

// Take 2 hours (in seconds) away from current timestamp
$two_hours_ago = $time_now - (60 * 2);

// Display calculated value
echo "Two hours ago: " . date("Y-m-d H:i:s", $two_hours_ago);

上述内容也可以作为单行代码完成:

e.g。

echo echo "Two hours ago: " . date("Y-m-d H:i:s", time() - (60 * 2));