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?
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.
To round the Timedelta with specified resolution, use the timestamp. round() method. Set the seconds frequency resolution using the freq parameter with value 's'.
Try this,
$date = "2013-04-20 16:25:34"; echo date("Y-m-d H:00:00",strtotime($date));
CodePad Demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With