Laravel检查当前时间介于两次之间



为了解决这个问题,我在网上尝试了各种解决方案,但结果总是一样的:false

我正试图准确检查当前时间是否在10pm早上6点之间,但所有解决方案都返回false!!

例如,如果我在8 am13 pm之间进行检查,它会起作用!但只是它不适用于我前面给出的例子。有解决方案吗?

编辑:这是我尝试的一个例子

@php
$now = Carbon::now();
$start = Carbon::createFromTimeString("22:00 pm")
$end = Carbon::createFromTimeString("06:00 am");
@endphp

@if ($now->between($start, $end))
//approved
@else
//not approved
@endif

更新:问题终于解决了,下面是那些面临或将在未来面临此问题的人的代码:

@php
$now = Carbon::now();
$curr = date('a'); //return am or pm
$start = Carbon::createFromTimeString("22:00"); //init the start time
$end = Carbon::createFromTimeString("06:00"); //init the end time
if ($curr == 'am'){
$start = Carbon::createFromTimeString("22:00")->subDay();
//check if we are in the morning or after midnight , and go to yesterday datetime
}
else{
//then  we are in the evening or afternoon  , so add a day to get tomorrow's datetime
$end = Carbon::createFromTimeString("06:00)->addDay();
}
@endphp

@if ($now->between($start, $end,true))
//approved
@else
//not approved
@endif

附言:我确信我的代码没有错,我已经验证了好几天了

问题的解决方案:

@php
$now = Carbon::now();
$curr = date('a'); //return am or pm
$start = Carbon::createFromTimeString("22:00"); //init the start time
$end = Carbon::createFromTimeString("06:00"); //init the end time
if ($curr == 'am'){
$start = Carbon::createFromTimeString("22:00")->subDay();
//check if we are in the morning or after midnight , and go to yesterday datetime
}
else{
//then  we are in the evening or afternoon  , so add a day to get tomorrow's datetime
$end = Carbon::createFromTimeString("06:00)->addDay();
}
@endphp

@if ($now->between($start, $end,true))
//approved
@else
//not approved
@endif

最新更新