Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

php-carbon

$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?

like image 241
Chris Avatar asked Jul 26 '17 15:07

Chris


1 Answers

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)) {
    // ¯\_(ツ)_/¯
}
like image 77
AliN11 Avatar answered Oct 11 '22 11:10

AliN11