0

Hello i want to check if the current time is between two time range and calculate the difference between them, so far i have this but its not working

$current_time = "11:14 pm";
$start_time = "11:00 pm";
$end_time = "07:55 am";
$date1 = DateTime::createFromFormat('H:i a', $current_time);
$date2 = DateTime::createFromFormat('H:i a', $start_time);
$date3 = DateTime::createFromFormat('H:i a', $end_time);
if ($date1 > $date2 && $date1 < $date3) {
   echo 'in range';
} else {
    echo 'not in range';
}

But it says "not in range"!

Sam
  • 47
  • 9
  • `$date1` and 2 and 3 are not integers – Ôrel Mar 02 '15 at 12:11
  • 1
    @Ôrel - DateTime objects explicitly permit this type of comparison, even though they aren't integers.... see the note about comparing objects on this [PHP Docs page](http://php.net/manual/en/language.operators.comparison.php) – Mark Baker Mar 02 '15 at 12:17
  • http://stackoverflow.com/questions/961074/how-do-i-compare-two-datetime-objects-in-php-5-2-8 – Andy Fleming Mar 02 '15 at 12:27
  • Your logic is off. `$date1` will be more than `$date2` but it will never be less than `$date3` at the same time. Not with the times you've supplied us. 11:14 pm is NOT LESS than 07:55 am, it's more. – Funk Forty Niner Mar 02 '15 at 12:44

2 Answers2

3

The main issue with your original code is that you are having it create dates from 3 times with unexpected results.

The start of the range is "11:00p" which it assumes means today at 11p.

The end of the range is "7:00a" which is assumes is also today. You actually intend to say "tomorrow at 7:00a".


You could try using strtotime.

$currentTime = strtotime("11:14 pm");
$rangeStart = strtotime("11:00 pm");
$rangeEnd = strtotime("tomorrow 07:55 am");

if ($currentTime >= $rangeStart && $currentTime <= $rangeEnd) {
   echo 'in range';
} else {
    echo 'not in range';
}

Or you could include actual dates and do something like this:

$currentTime = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-01 23:14:00");
$rangeStart = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-01 23:00:00");
$rangeEnd = DateTime::createFromFormat("Y-m-d H:i:s", "2015-01-02 07:55:00");

if ($currentTime >= $rangeStart && $currentTime <= $rangeEnd) {
   echo 'in range';
} else {
    echo 'not in range';
}
Andy Fleming
  • 7,655
  • 6
  • 33
  • 53
1

When start is after end you need to deal with a day change.

$current_time = "11:14 pm"; 
$start_time = "11:00 pm"; 
$end_time = "07:55 am"; 
$date1 = DateTime::createFromFormat('H:i a', $current_time)->getTimestamp(); 
$date2 = DateTime::createFromFormat('H:i a', $start_time)->getTimestamp();; 
$date3 = DateTime::createFromFormat('H:i a', $end_time)->getTimestamp(); 
if ($date3 < $date2) { 
    $date3 += 24 * 3600; 
    if ($date1 < $date2) { 
        $date1 += 24 *3600; 
    } 

} 
if ($date1 > $date2 && $date1 < $date3) { 
   echo 'in range'; 
} else { 
    echo 'not in range'; 
} 
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • 1
    Adding the day is the correct solution; but why can't you do this purely with DateTime objects? – Mark Baker Mar 02 '15 at 12:23
  • `$current_time = "11:14 pm"; $start_time = "11:00 pm"; $end_time = "07:55 am"; $date1 = DateTime::createFromFormat('H:i a', $current_time); $date2 = DateTime::createFromFormat('H:i a', $start_time); $date3 = DateTime::createFromFormat('H:i a', $end_time); if ($date3 < $date2) { $date3->add(new DateInterval('P1D')); if ($date1 < $date2) { $date1->add(new DateInterval('P1D')); } } if ($date1 > $date2 && $date1 < $date3) { echo 'in range'; } else { echo 'not in range'; } ` – Mark Baker Mar 02 '15 at 12:27
  • Great answer, just one more question how to calculate time difference between current time and end time, ex: current time is 11:30 pm and end time is 7:55 am, i want to get the difference of those two times in minutes, can you help me please? thanks – Sam Mar 02 '15 at 12:54
  • with timestamp just do the diff the answer is in second. – Ôrel Mar 02 '15 at 13:01