Search code examples
phptimetimercountdown

Php Counting seconds to the nearest quarter-hour


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?


Solution

  • 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...