Jump to content

Recommended Posts

Hey Guys. I am new to using dates in PHP and have been playing a lot with the PHP functions that are available for the date and time. t

 

But I am stuck  on trying to figure out, how to display the minutes of the current time, so that it will round to the nearest  quarter of hour, based on the current time. Example would be:

 

1:15

1:30

1:45

 

I have a came a cross a function by doing some googling, and have came across the following function.

function roundToQuarterHour($timestring) {
    $minutes = date('i', strtotime($timestring));
    return $minutes - ($minutes % 15);
}

I then tried to implement doing with the following

$time =  time();
function roundToQuarterHour($time) {
    $minutes = date('i', strtotime($time));
    return $minutes - ($minutes % 15);
}
$rounded_time = roundToQuarterHour($time);
echo $rounded_time; 


It doesn't display anything. When i did a var_dump on the $rounded_time i got a int(0)

 

Not sure on how to go about this....Any help would really be appreciated!


<?php
echo roundToQuarterHour('2014-04-03 10:06:33'); // 2014-04-03 10:00:00
echo roundToQuarterHour('2014-04-03 10:11:33'); // 2014-04-03 10:15:00
echo roundToQuarterHour('2014-04-03 10:18:33'); // 2014-04-03 10:15:00
echo roundToQuarterHour('2014-04-03 10:24:33'); // 2014-04-03 10:30:00

function roundToQuarterHour($tstr) {
$t = round(strtotime($tstr)/900)*900;
return date('Y-m-d H:i:s', $t);
}

?>

Hey Barand. Thank you so much for providing me a working example. But for future references would be able to explain me how you did the math? Just so that I can get an idea on how to implement in the future. That would be really helpful for me then just copying and pasting. Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.