Home > other >  PHP Laravel Comparison Date(Hour) [duplicate]
PHP Laravel Comparison Date(Hour) [duplicate]

Time:09-17

I have 2 hours:

$night = "19:00";
$morning = "04:00"; (tomorrow)

What i want is, if hour now is between 19:00 - 04:00 the result is true,how can i achieve that? Sorry for my broken english, Thankyou in advance

CodePudding user response:

Try this using DateTime.

$now = new \DateTime();
$current = $now->format('Y-m-d H:i');
$night= $now->format('Y-m-d 17:00');
$morning = $now->modify(' 1 day')->format('Y-m-d 04:00');
                
if($night <= $now && $now <= $morning) {
  echo "Yes";
}else{
  echo "No";
}

CodePudding user response:

Try to use strotime() function from PHP

$now = date("Y-m-d H:i:s");
$night = "2021-09-08 19:00:00";
$morning = "2021-09-09 04:00:00";
if(strtotime($night)<= strtotime($now) && strtotime($now) <= strtotime($morning)) {
    //do some work
} else {
    //do something
}
  • Related