'php carbon check if now is between two times (10pm-8am)

$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');

How can I check if the time of $now is within the timerange?



Solution 1:[1]

There are several ways to achieve that by using Carbon. One of the easiest ways is using createFromTimeString and between methods:

$now = Carbon::now();

$start = Carbon::createFromTimeString('22:00');
$end = Carbon::createFromTimeString('08:00')->addDay();

if ($now->between($start, $end)) {
    // ¯\_(?)_/¯
}

Solution 2:[2]

Try this:

  $time = Carbon::now();
  $morning = Carbon::create($time->year, $time->month, $time->day, 8, 0, 0); //set time to 08:00
  $evening = Carbon::create($time->year, $time->month, $time->day, 18, 0, 0); //set time to 18:00
  if($time->between($morning, $evening, true)) {
    //current time is between morning and evening
  } else {
    //current time is earlier than morning or later than evening
  }

The true in $time->between($morning, $evening, true) checks whether the $time is between and including $morning and $evening. If you write false instead it checks just if it is between the two times but not including.

Actually, you could leave true away because it is set by default and not needed.

Check here for more information on how to compare dates and times with Carbon.

Solution 3:[3]

$start = '22:00:00';
$end   = '08:00:00';
$now   = Carbon::now('UTC');
$time  = $now->format('H:i:s');

if ($time >= $start && $time <= $end) {
    ...
}

Should do it, but doesn't take date into consideration

Solution 4:[4]

You can reverse check algorithm.

<?php
$pushChannel = "general";

$now = Carbon::now();

$start = Carbon::createFromTime(8, 0);
$end = Carbon::createFromTime(22, 0);

if (!$now->between($start, $end)) {
     $pushChannel = "silent";

Solution 5:[5]

$restrictStartTime = Carbon::createFromTime(22, 0, 0);  //carbon inbuild function which will create todays date with the given time
$restrictEndTime = Carbon::createFromTime(8, 0, 0)->addDays(1); //this will create tomorrows date with the given time
$now = Carbon::now();

if($now->gt($restrictStartTime) && $now->lt($restrictEndTime)) {
 .....
 }

Solution 6:[6]

Please Try below code,

$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');

$nowTime = $now->hour.':'.$now->minute.':'.$now->second;

if(strtotime($nowTime) > strtotime($start) && strtotime($nowTime) < strtotime($end) ) {
    echo 'YES';
} else {
    echo 'NO';
}

Solution 7:[7]

What Chris is trying to point out is if the endtime crosses over midnight then you must account for that.

This is not the cleanest way to do it but here is a method that seems to work.

private function isNowBetweenTimes($timezone, $startDateTime, $endDateTime) {
    $curTimeLocal = Carbon::now($timezone);
    $startTime = $curTimeLocal->copy();
    $startTime->hour = $startDateTime->hour;
    $startTime->minute = $startDateTime->minute;
    $endTime = $curTimeLocal->copy();
    $endTime->hour = $endDateTime->hour;
    $endTime->minute = $endDateTime->minute;
    if ($endTime->lessThan($startTime))
        $endTime->addDay();

    return ($curTimeLocal->isBetween($startTime, $endTime));
}

This example only cares about the hour and minutes and not the seconds but you can easily copy that as well. The key to this is comparing start and end time before comparing them to the current time and add a day to end time if end time is less than start time.

Solution 8:[8]

For complete solution which supports all start and end time range you can use bitwise XOR.

/*
 * must using hours in 24 hours format e.g. set 0 for 12 pm, 6 for 6 am and 13 for 1 pm
 */
private $startTime = '0';
private $endTime = '6';
    

$currentHour = \Carbon\Carbon::now()->hour;
$start = $this->startTime > $this->endTime ? !($this->startTime <= $currentHour) : $this->startTime <= $currentHour;
$end = $currentHour < $this->endTime;


if (!($start ^ $end)) {
  //Do stuff here if you want exactly between start and end time
}

Solution 9:[9]

<?php
$now = date("H");

if ($now < "20") {
    echo "Have a good day!";
}

Solution 10:[10]

Try this :

$start = 22; //Eg. start hour
$end = 08;  //Eg. end hour
$now = Carbon::now('UTC');

if( $start < $now->hour && $now->hour < $end){
    // Do something
}

Solution 11:[11]

@AliN11's (currently top) answer is good, but doesn't work as one would immediately expect, after midnight it just breaks, as raised in the comments by @Sasha

The solution is to reverse the logic, and check if the time is not between the inverse hours.

Here is an alternative that works as one would expect:

    $now = Carbon::now();
    $start = Carbon::createFromTimeString('08:00');
    $end = Carbon::createFromTimeString('22:00');

    if (! $now->between($start, $end)) {
        // We're all good
    }

Solution 12:[12]

Yes, the midnight plays a vital role in time duration. We can find now() being the given time range as follows:

$now = Carbon::now();
$start = Carbon::createFromTime('22', '00');
$end = Carbon::createFromTime('08', '00');

if ($start->gt($end)) {
    if ($now->gte($start)) {
        $end->addDay();
    } elseif ($now->lte($end)) {
        $start->subDay();
    } else {
        return false;
    }
}

return $now->between($start, $end);