Php mysql时间戳 - 2小时[关闭]

I have this working for the current time in PHP:

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

I cant figure out how to append this to have the current time -2 hours in this same format. Can anyone help me out quick? Thank you!

You could use strtotime():

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

The strtotime() function returns a timestamp, which you can then pass to date() to get the appropriately formatted date.

Here you are:

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

Hope it helps ;)

Link: PHP.net strtotime()

You can also manipulate the unix timestamp manually; because the timestamps are in seconds:

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);

The above can be done as a one-liner also:

e.g.

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