Rob Rob - 4 years ago 142
PHP Question

Round minute down to nearest quarter hour

I need to round times down to the nearest quarter hour in PHP. The times are being pulled from a MySQL database from a datetime column and formatted like

2010-03-18 10:50:00
.

Example:


  • 10:50 needs to be 10:45

  • 1:12 needs to be 1:00

  • 3:28 needs to be 3:15

  • etc.



I'm assuming
floor()
is involved but not sure how to go about it.

Thanks

Answer Source

Your full function would be something like this...

function roundToQuarterHour($timestring) {
    $minutes = date('i', strtotime($timestring));
    return $minutes - ($minutes % 15);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download