Check time in range php

I have to check if the current daytime falls in a specific range. I looked up the internet and found several similar solutions like this one:

$now = date("His");//or date("H:i:s")

$start = '130000';//or '13:00:00'
$end = '170000';//or '17:00:00'

if($now >= $start && $now <= $end){
echo "Time in between";
}
else{
echo "Time outside constraints";
}

If both conditions have to be true, how can this bis achieved when we assume that $start is 06:00:00 and $end is 02:00:00.

If we make the assumption that it is 01:00:00, in this case the first condition can't be true.

Has anybody an idea to handle this problem differently?

Thanks!

asked Nov 17, 2014 at 17:37

2

Naturally, you'd have to account for date in your comparisons.

= $start && time() <= $end) {
  // ok
} else {
  // not ok
}

answered Nov 17, 2014 at 17:54

ceejayozceejayoz

173k40 gold badges288 silver badges358 bronze badges

If you need to check whether or not the time frame rolls over midnight

    function isWithinTimeRange($start, $end){

        $now = date("His");

        // time frame rolls over midnight
        if($start > $end) {

            // if current time is past start time or before end time

            if($now >= $start || $now < $end){
                return true;
            }
        }

        // else time frame is within same day check if we are between start and end

        else if ($now >= $start && $now <= $end) {
            return true;
        }

        return false;
    }

You can then get whether or not you are within that time frame by

echo isWithinTimeRange(130000, 170000);

answered Dec 27, 2016 at 6:30

Check time in range php

DOfficialDOfficial

4753 silver badges20 bronze badges

1

date_default_timezone_set("Asia/Colombo");
            $nowDate = date("Y-m-d h:i:sa");
            //echo '
' . $nowDate; $start = '21:39:35'; $end = '25:39:35'; $time = date("H:i:s", strtotime($nowDate)); $this->isWithInTime($start, $end, $time); function isWithInTime($start,$end,$time) { if (($time >= $start )&& ($time <= $end)) { // echo 'OK'; return TRUE; } else { //echo 'Not OK'; return FALSE; } }

answered Oct 17, 2017 at 15:19

Check time in range php

RavinathRavinath

1,45012 silver badges8 bronze badges

Cannot comment due to low reputation, but @DOfficial answer is great but be aware of inconsistency in comparision.

Original

 // if current time is past start time or before end time
 if($now >= $start || $now < $end){

Should be imho

 // if current time is past start time or before end time
 if($now >= $start || $now <= $end){

answered Nov 11, 2020 at 13:57

    Table of contents
  • PHP check if time falls within range, questioning common solution
  • PHP check if time range is in a time range
  • PHP Check if current date is between two dates example
  • Test 2 time ranges to see if they overlap

PHP check if time falls within range, questioning common solution

$now = date("His");//or date("H:i:s")

$start = '130000';//or '13:00:00'
$end = '170000';//or '17:00:00'

if($now >= $start && $now <= $end){
echo "Time in between";
}
else{
echo "Time outside constraints";
}
= $start && time() <= $end) {
  // ok
} else {
  // not ok
}
    function isWithinTimeRange($start, $end){

        $now = date("His");

        // time frame rolls over midnight
        if($start > $end) {

            // if current time is past start time or before end time

            if($now >= $start || $now < $end){
                return true;
            }
        }

        // else time frame is within same day check if we are between start and end

        else if ($now >= $start && $now <= $end) {
            return true;
        }

        return false;
    }
echo isWithinTimeRange(130000, 170000);
date_default_timezone_set("Asia/Colombo");
            $nowDate = date("Y-m-d h:i:sa");
            //echo '
' . $nowDate; $start = '21:39:35'; $end = '25:39:35'; $time = date("H:i:s", strtotime($nowDate)); $this->isWithInTime($start, $end, $time); function isWithInTime($start,$end,$time) { if (($time >= $start )&& ($time <= $end)) { // echo 'OK'; return TRUE; } else { //echo 'Not OK'; return FALSE; } }
 // if current time is past start time or before end time
 if($now >= $start || $now < $end){
 // if current time is past start time or before end time
 if($now >= $start || $now <= $end){

PHP check if time range is in a time range

$van_besch = '18:00';
$tot_besch = '22:00';

$van = '13:00';
$tot = '19:00';

if (($van_besch >= $van) && ($tot_besch <= $tot)) {
        $error ='Time '.$van_besch.' - '.$tot_besch.' Not available';
    }
if ($tot < $van){
   $tot = 24:00 - $van + $tot;
}

if ($tot_besch < $van_besch){
   $tot = 24:00 - $van + $tot;
}

if (($van >= $van_besch) && ($van <= $tot_besch) ) || (($tot >= $tot_besch) && ($tot <= $tot_besch)){
 ... Peter is not available ... 
}
$van_besch = '18:00';
$tot_besch = '22:00';

$van = '13:00';
$tot = '19:00';

if (strtotime($van) < strtotime($van_besch) || strtotime($tot ) > strtotime($tot_besch)) {
   //code goes here
}
CREATE TABLE off_time (
  id int(12) AUTO_INCREMENT,
  employee int(12) NOT NULL,
  start_time smallInt(4) unsigned NOT NULL,
  end_time smallInt(4) unsigned NOT NULL,
  PRIMARY KEY (id)
);
// Here we use just < and > but you can make them <= and >= if you don't want time slots to possibly overlap start/end times.
SELECT ot.id, IF( 1300 < ot.end_time AND 1900 > ot.start_time, 1, 0) AS blocked
FROM off_time as ot
WHERE ot.employee = 1; 

PHP Check if current date is between two dates example

= $startDate) && ($currentDate <= $endDate)){    echo "Current date is between two dates";}else{    echo "Current date is not between two dates";  }
Current date is between two dates

Test 2 time ranges to see if they overlap

$start_time1 = "10:15";
$end_time1 = "12:20";

$start_time2 = "13:15";
$end_time2 = "14:25";

function testRange($start_time1,$end_time1,$start_time2,$end_time2)
{
    $timeCheck;

    if(($end_time1 < $start_time2))
    {
        $timeCheck = true;
        return $timeCheck;
    }
    else if(($start_time1 > $start_time2) && ($start_time1 > $end_time2)) 
    {
        $timeCheck = true;
        return $timeCheck;
    }
    else
    {
        $timeCheck = false;
        return $timeCheck;
    }

}

if(testRange($start_time1,$end_time1,$start_time2,$end_time2))
{
    echo "Not In Range";
}
else
{
    echo "In Range";
}
             1
Case 01:  |-----|
          |-----|
             2

             1
Case 02:  |-----|
             |-----|
                2

                1
Case 03:     |-----|
          |-----|
             2

               1
Case 04:  |---------|
            |-----|
               2

               1
Case 05:    |-----|
          |---------|
               2

             1
Case 06:  |-----|
                  |-----|
                     2

                     1
Case 07:          |-----|
          |-----|
             2

             1
Case 08:  |-----|
                |-----|
                   2

                   1
Case 09:        |-----|
          |-----|
             2

             1
Case 10:     |
          |-----|
             2

             1
Case 11:  |-----|
             |
             2

          1
Case 12:  |
          |-----|
             2

                1
Case 13:        |
          |-----|
             2

             1
Case 14:  |-----|
          |
          2

             1
Case 15:  |-----|
                |
                2

          1
Case 16:  |
          |
          2

        |                         |    Operlap    |     Overlap     
  Case  |         Example         | Open Interval | Closed Interval 
--------|-------------------------|---------------|-----------------
Case 01 | 09:00-11:00 09:00-11:00 |      Yes      |        Yes
Case 02 | 09:00-11:00 10:00-12:00 |      Yes      |        Yes
Case 03 | 10:00-12:00 09:00-11:00 |      Yes      |        Yes
Case 04 | 09:00-12:00 10:00-11:00 |      Yes      |        Yes
Case 05 | 10:00-11:00 09:00-12:00 |      Yes      |        Yes
Case 06 | 09:00-10:00 11:00-12:00 |      No       |        No
Case 07 | 11:00-12:00 09:00-10:00 |      No       |        No
Case 08 | 09:00-10:00 10:00-11:00 |      No       |        Yes
Case 09 | 10:00-11:00 09:00-10:00 |      No       |        Yes
Case 10 | 10:00-10:00 09:00-11:00 |      Yes      |        Yes
Case 11 | 09:00-11:00 10:00-10:00 |      Yes      |        Yes
Case 12 | 09:00-09:00 09:00-10:00 |      No       |        Yes
Case 13 | 10:00-10:00 09:00-10:00 |      No       |        Yes
Case 14 | 09:00-10:00 09:00-09:00 |      No       |        Yes
Case 15 | 09:00-10:00 10:00-10:00 |      No       |        Yes
Case 16 | 09:00-09:00 09:00-09:00 |      No       |        Yes
function rangesNotOverlapClosed($start_time1,$end_time1,$start_time2,$end_time2)
{
  $utc = new DateTimeZone('UTC');

  $start1 = new DateTime($start_time1,$utc);
  $end1 = new DateTime($end_time1,$utc);
  if($end1 < $start1)
    throw new Exception('Range is negative.');

  $start2 = new DateTime($start_time2,$utc);
  $end2 = new DateTime($end_time2,$utc);
  if($end2 < $start2)
    throw new Exception('Range is negative.');

  return ($end1 < $start2) || ($end2 < $start1);
}

function rangesNotOverlapOpen($start_time1,$end_time1,$start_time2,$end_time2)
{
  $utc = new DateTimeZone('UTC');

  $start1 = new DateTime($start_time1,$utc);
  $end1 = new DateTime($end_time1,$utc);
  if($end1 < $start1)
    throw new Exception('Range is negative.');

  $start2 = new DateTime($start_time2,$utc);
  $end2 = new DateTime($end_time2,$utc);
  if($end2 < $start2)
    throw new Exception('Range is negative.');

  return ($end1 <= $start2) || ($end2 <= $start1);
}
function testRange($start_time1,$end_time1,$start_time2,$end_time2)
{
  return ($start_time1 <= $end_time2 && $start_time2 <= $end_time1);
}

Next Lesson PHP Tutorial