0

I need to be able to check if a time is within 15 minutes of specified time over days.

But my issue is that it is only comparing minute and not days or years or months.

EDIT: I have updated my code but it still does not work with previous days.

$notification_time = strtotime("-1 hour", $cal_time);
$really_now = time();
if($really_now >= strtotime("-15 minutes", $notification_time) && $really_now <= strtotime("+15 minutes", $notification_time))
{ echo 'within time!'; }

The above code works if $really_now is within 30 minutes of -1 hour of $cal_time. But when I try:

$notification_time = strtotime("-1 day", $cal_time);

If the current time is within 30 minutes of $cal_time -1 day it will not work.

  • You need to convert the time to unix time (expressed in seconds) and 15 minutes into seconds and work the math in seconds. When you get the result you convert it to hours, days, minutes, whatever, – Đuro Mandinić Mar 10 '14 at 21:32
  • try work with gmdate(); – Nazaret2005 Mar 10 '14 at 21:32
  • I don't care about the output. My concern is the above code does not check the days (if -1 hour returned a different day than the $cal_time), because I also want to replace "-1 hour" with "-1 day" "-3 day" ect. – Leonard Washington Mar 10 '14 at 22:23

3 Answers3

0
<?php

$cur_time = time();
$from_time = strtotime("-1 hour", $cur_time);
echo round(abs($cur_time - $from_time) / 60,2). " minute";

?>

This will provide the difference in minutes. Please note this was similarly answered in this post:

How to get time difference in minutes in PHP

Community
  • 1
  • 1
binaryNomad
  • 336
  • 2
  • 6
  • This is not the issue I am experiencing or I may be missing something. I have no problem calculating the time between two dates. Please view my edited question for more information. – Leonard Washington Mar 11 '14 at 14:53
0

My answer..

$notification_time = strtotime("-1 day", $cal_time);
$really_now = time();
if($really_now >= strtotime("-15 minutes", $notification_time) && $really_now <= strtotime("+15 minutes", $notification_time)){ echo 'shazam!'; }
0

This snippet I used to check restaurant closing time and opening time range. A restaurant can be night club also that can open at 8 PM and closes 2 AM. All cases(forward range and reverse range) are true . format of parameters is only time format. No date. put current_order_time=0 in parameter as there is no waiting time.

echo isOnline('22:59:00','00:59:59','23:39:01',81);
function isOnline($open_time,$close_time,$now, $current_order_time){
 $live=1;
 $open=strtotime($open_time);
 $close=strtotime($close_time);
 //echo $open ."<". $close. "<br>" ;
 if($open > $close){
  $now=strtotime(date("Y-m-d ").$now);
  $now_collection=$now +($current_order_time*60);
  $open=strtotime(date('Y-m-d').' '.$open_time);
  $close=strtotime('+1 day',strtotime(date('Y-m-d').' '.$close_time));
  //echo $open ."<". $now_collection ."&&". $close .">". $now_collection ."&&". $open ."<". $now ."&&". $close .">". $now."<br>";
  if(!($open < $now_collection && $close > $now_collection && $open < $now && $close > $now )){
   //return $this->getFinalFailure("The restaurant is currently Off-line and not taking orders",2000);
   $live=0;
  }
  
 }else{
  $now=strtotime($now);
  $now_collection= $now + ($current_order_time*60);
  //echo $open ."<". $now_collection ."&&". $close .">". $now_collection ."&&". $open ."<". $now ."&&". $close .">". $now."<br>";
  if(!($open < $now_collection && $close > $now_collection && $open < $now && $close > $now )){
   $live=0; 
  }
 
 }
    return $live;
}