0

I'm working on a tick based space game http://ricarion.com/ but the ticks only run between certain hours.

08:00-16:30 - run every 30 minutes via a cron job. In the nav bar at the top I want to add "Next Tick: 08:30 06/02/20" for example.

So I was thinking of creating an array:

$tick_times[] = array();
$tick_times[] = 08:00;
$tick_times[] = 08:30;
$tick_times[] = 09:00;
...
$tick_times[] = 16:30;

And then this is where I get stuck, how do I check the existing time, and then compare that against the array selecting the next future time? i.e. It's now 08:34, so the return should be 09:00?

2 Answers2

1

Did you need an array or just want to calculate the next 30-minute interval?

If so this may be similar to: Round minute down to nearest quarter hour

You do modulo division of 1800 seconds on the current time and add the answer (time remainder of time to the next interval) to the then-current time to get the next event.

<?php
    $current_date = date('d-M-Y g:i:s A');
    echo $current_date."\n";
    $current_time = strtotime($current_date);

    $frac = 1800;
    $r = $current_time % $frac;

    $new_time = $current_time + ($frac-$r);
    $new_date = date('d-M-Y g:i:s A', $new_time);

    echo $new_date."\n";

http://codepad.org/xs9lMCRQ

0

Get the now time format it and compare it. In your case you maybe format your $tick_time to the same format like current time.

$date = new DateTime('now');
$date = $date->format('Y-m-d H:i:s');
foreach ($tick_times as $tick_time) {
    $date_added = new DateTime($tick_time);
    if (strtotime($date_added) == strtotime($date)) {
       //do your stuff here
    }
}
K. B.
  • 1,388
  • 2
  • 13
  • 25
  • It was just looking for a way to echo out my next tick time, rather than do something at tick time. Nerds of Technology has given me enough to get on the right path. Thanks for your answer. – Dave's Undies Feb 06 '20 at 11:15
  • if my suggestion help somehow, you can mark the answer as usefull. – K. B. Feb 06 '20 at 11:31