Getting a timestamp for today at midnight?

129

How would I go about getting a timestamp in php for today at midnight. Say it's monday 5PM and I want the Timestamp for Monday(today) at midnight(12 am) which already has happened.

Thank you

This question is tagged with php time timestamp

~ Asked on 2012-10-29 21:16:31

The Best Answer is


252

$timestamp = strtotime('today midnight');

You might want to take a look what PHP has to offer: http://php.net/datetime

~ Answered on 2012-10-29 21:20:04


77

I think that you should use the new PHP DateTime object as it has no issues doing dates beyond the 32 bit restrictions that strtotime() has. Here's an example of how you would get today's date at midnight.

$today = new DateTime();
$today->setTime(0,0);

Or if you're using PHP 5.4 or later you can do it this way:

$today = (new DateTime())->setTime(0,0);

Then you can use the echo $today->format('Y-m-d'); to get the format you want your object output as.

PHP DateTime Object

~ Answered on 2014-03-20 14:16:07


Most Viewed Questions: