Skip to content
Advertisement

Round datetime to last hour

I tried to look for this but I could not find good example of this what im trying to do.

I got datetime values in MySQL database that has to be rounded down when that value is on use.

Example, all these values:

2013-04-20 07:14:42
2013-04-20 07:19:51
2013-04-20 07:37:26
2013-04-20 07:46:28
2013-04-20 07:59:44

Should be rounded down to:

2013-04-20 07:00:00

And

2013-04-20 16:25:34

should be:

2013-04-20 16:00:00 etc…

PHP code that gets date value:

$d = strtotime($row["date"]);

So, how its possible to round down datetime value?

Advertisement

Answer

Try this,

$date = "2013-04-20 16:25:34"; 
echo date("Y-m-d H:00:00",strtotime($date));

CodePad Demo.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement