Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round datetime to last hour

Tags:

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?

like image 345
plexcell Avatar asked Apr 20 '13 09:04

plexcell


People also ask

How do you round the date to the nearest hour?

Round time to nearest hour ( TIME(1,0,0) = 1/24 representing an hour) and add a zero time value to ensure the expression is cast as a Time. M: There isn't an equivalent of MROUND in M, so instead multiply the time by 24 to express it as a number of hours, then round, then divide by 24 and convert to a Time type.

How do you round off seconds in Python?

To round the Timedelta with specified resolution, use the timestamp. round() method. Set the seconds frequency resolution using the freq parameter with value 's'.


1 Answers

Try this,

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

CodePad Demo.

like image 140
Rikesh Avatar answered Oct 21 '22 07:10

Rikesh