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

Chris picture Chris · Jul 26, 2017 · Viewed 19.1k times · Source
$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?

Answer

AliN11 picture AliN11 · Jan 2, 2020

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)) {
    // ¯\_(ツ)_/¯
}