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"!

4
  • $date1 and 2 and 3 are not integers Commented Mar 2, 2015 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 Commented Mar 2, 2015 at 12:17
  • stackoverflow.com/questions/961074/… Commented Mar 2, 2015 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. Commented Mar 2, 2015 at 12:44

2 Answers 2

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'; } 
Sign up to request clarification or add additional context in comments.

Comments

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'; } 

4 Comments

Adding the day is the correct solution; but why can't you do this purely with DateTime objects?
$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'; }
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
with timestamp just do the diff the answer is in second.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.