2

I've got an application that requires the time until an event (in seconds) to display a countdown timer. The countdown timer should be counting down 20 minutes at a time. However, I need to use php to find the seconds to the next third of an hour (12:00, 12:20, 12:40, etc) and I've got no idea where to go with the logic on this.

Anyone have suggestions?

Eric
  • 2,201
  • 5
  • 29
  • 35
  • http://stackoverflow.com/questions/2480637/round-minute-down-to-nearest-quarter-hour – Ben Swinburne Nov 03 '11 at 17:32
  • and when the counter is finhed counting how you will send back data ?? or how you will make php wait ? you will make the user wait for the page ? i suggest you echo the data down and use javascript instead – Qchmqs Nov 03 '11 at 17:33
  • Just as a note, 20 minutes is a third of an hour, not a quarter. – jprofitt Nov 03 '11 at 17:47
  • Ben, I don't need to round to the nearest, I need to round up to the next. @Qchmqs I am not sending back any data. This is simply to display the time until a next event. There is a cron running server-side that takes care of this. This countdown timer is simply something for the end-user. Also, jprofitt is right. I fail at math. – Eric Nov 03 '11 at 17:48
  • You asked for the logic about how to achieve it. All of which was contained in the question/answer in the link I posted. Granted you didn't need to round but the information was there. See my answer. – Ben Swinburne Nov 03 '11 at 18:43

2 Answers2

7
echo $remaining = 15-(date('i', time()) % 15);

$remaining is the remainder of the number of minutes passed the hour divided by 15. Then as you want to see how many until the next 15, 15-x is your answer.

  • So 18:41 is 41 minutes past the hour.
  • 41%15 gives a remainder of 11.
  • 15-11 gives you 4 meaning there are 4 minutes until the next 15 minute interval.

Obviously change 15 to 20 if you want thirds rather than quarters of an hour...

Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
0

You can ask php how late it is, and to return the minutes only. You do this with the function date(). Then you make simple if-function (I wrote it as a function, but you can also use it as 'normal' consecutive php, without the function and return stuff). Substract the minutes we are from the minutes you want to go to, and make seconds of them.

Like this:

<?php
function get_the_seconds() { //


$minutes = date('i'); //how many minutes in the hour are we

   if ($minutes <= 20) {
       $seconds = (20-$minutes)*60; //60 secs in a minute :)
   } elseif ($minutes >= 20 && $minutes <= 40) {
       $seconds = (40-$minutes)*60;
   } else {
       $seconds = (60-$minutes)*60;
   }
   return $seconds 
}
?>
NikM
  • 83
  • 1
  • 8