Round datetime to last hour
If you are using PHP's Carbon DateTime library (which you really should - https://github.com/briannesbitt/Carbon )
You can achieve it easily using
Carbon::now()->minute(0)->second(0);
Since you already have a Unix timestamp $d
, most efficient way is to use only arithmetic instead of date functions - especially if you're going to loop through a result set.
$hourTimestamp = $d - ($d % 3600);
Modulo operator gives you the remainder which you subtract from the timestamp to get hour timestamp.
In that case a simple substr
could do:
echo substr($date, 0, 13) . ":00:00";
Try this,
$date = "2013-04-20 16:25:34";
echo date("Y-m-d H:00:00",strtotime($date));
CodePad Demo.