Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if a time period matches the current time

I have a date like this:

 // just a example date
 $time_in_the_future_start = date('H:i', time()+60*60*24*7);
 $time_in_the_future_end = date('H:i', time()+60*60*24*7+480);

 $now = date('H:i', time());

How can I find out if the current time is within $time_in_the_future's time?

For example if

$time_in_the_future_start = 21:30;
$time_in_the_future_end = 0:30;

and

$now = 22:07

it would be a match.

edit: to clarify, the $time_in_the_future is a recurring event, with a date in the past or future. I only need the hours+minutes to match, not the rest of the date... Basically I just want to compare the time in a day

like image 306
Alex Avatar asked Dec 08 '25 10:12

Alex


1 Answers

function inTimeRange()

I guess, this will work perfectly in your case.

  function inTimeRange($time_start, $time_end, $time_needle) {
    $res = false;
    $t1 = strtotime("1970-01-01 {$time_start}:00");
    $t2 = strtotime("1970-01-01 {$time_end}:00");
    $tn = strtotime("1970-01-01 {$time_needle}:00");
    if ($t1 >= $t2) $t2 = strtotime('+1 day', $t2);
    return ($tn >= $t1) && ($tn <= $t2); // or return ($tn > $t1) && ($tn < $t2);
    }

  var_dump(inTimeRange('21:30', '00:30', '22:07'));

Output

true

Note: All parameters must be in format HH:MM. You can pass date("H:i", ...) for parameters of this function.

like image 181
Wh1T3h4Ck5 Avatar answered Dec 10 '25 01:12

Wh1T3h4Ck5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!