比较 Carbon startOfDay() 与 now()->startOfDay() 不能正常工作

Comparing Carbon startOfDay() with now()->startOfDay() does not work properly

我想比较当前日期的开始日期和自定义日期的开始日期,从数据库中检索(这是一个 UNIX 时间戳。

所以我在控制器上编写了这个代码:

foreach($popups as $popup)
{ 
    $date = Carbon::createFromTimestamp($popup->datep);

    if($date->startOfDay()->eq(now()->startOfDay())){
        $result = true;
    }
    if($result == true){
        // show some data
    }
}

所以我用三个不同的日期对此进行了测试。 dd() 每个结果如下:

$date1 = Carbon::createFromTimestamp($popup[0]->datep);
dd($date1->startOfDay()); // returns date: 2021-11-08 00:00:00.0 Asia/Tehran (+03:30)

$date2 = Carbon::createFromTimestamp($popup[1]->datep);
dd($date2->startOfDay()); // returns date: 2021-11-09 00:00:00.0 Asia/Tehran (+03:30)

$date3 = Carbon::createFromTimestamp($popup[2]->datep);
dd($date3->startOfDay()); // returns date: 2021-11-10 00:00:00.0 Asia/Tehran (+03:30)

dd(now()->startOfDay());的结果是date: 2021-11-09 00:00:00.0 Asia/Tehran (+03:30)。这意味着只有 $popup[1]->datep 能够显示。

但现在的问题是,它也显示了第三个日期!但是,它的开始日期等于 10 而当前的开始日期是 11.

那么这里出了什么问题?

如何正确比较当前日期的开始日期与检索数据的开始日期?


完整代码:

    $output = "";
    $result = false;
    $titleshow = "";
    $popups = PopUp::all();
    
    if($popups->count() > 0)
    { 
        foreach($popups as $popup)
        { 
            $date = Carbon::createFromTimestamp($popup->datep);

            if($date->startOfDay()->eq(now()->startOfDay())){
                $result = true;
            }
            
            if($result == true){
                if($popup->showtitle == 1){
                    $titleshow = $popup->title;
                }
                $links = explode(",",$popup->linkp);
                $paths = explode(",",$popup->image_path);
                $matns = explode(",",$popup->matn);
                for($i=0;$i<=count($links)-1;$i++){
                    if(!empty($links[$i])){
                        $output .='<a href=" '.$links[$i].' "><img src=" '. URL::to('popups/'.$paths[$i]).' " style="width: 100%;"></a></br><p>'.$matns[$i].'</p></br>';
                    }else{
                        break;
                    }
                    
                }
            }
        }
    }
    json_encode($output);

然后在 Blade 中,我尝试将 $output 显示为 Sweet Alert 弹出消息。

您需要在每次迭代开始时“重置”$result 的状态:

foreach($popups as $popup)
{
    $result = false; 
    $date = Carbon::createFromTimestamp($popup->datep);

    if($date->startOfDay()->eq(now()->startOfDay())){
        $result = true;
    }
    if($result == true){
        // show some data
    }
}

一旦 $popup[1]->datep 被命中,$result 将设置为 true,无论您的情况如何 - 这就是 $popup[2]->datep 也会出现的原因。

编辑: 我会摆脱 $result 并在您的日期检查中进行“计算”:

<?php
$output = "";
$titleshow = "";
$popups = PopUp::all();

if($popups->count() > 0) { 
    foreach($popups as $popup) { 
        $date = Carbon::createFromTimestamp($popup->datep);
        if($date->startOfDay()->eq(now()->startOfDay())) {
            if($popup->showtitle == 1) {
                $titleshow = $popup->title;
            }
            $links = explode(",",$popup->linkp);
            $paths = explode(",",$popup->image_path);
            $matns = explode(",",$popup->matn);
            for($i=0; $i <= count($links)-1; $i++) {
                if(!empty($links[$i])) {
                    $output .='<a href=" '.$links[$i].' "><img src=" '. URL::to('popups/'.$paths[$i]).' " style="width: 100%;"></a></br><p>'.$matns[$i].'</p></br>';
                } else {
                    break;
                }
            }
        }
    }
}
echo json_encode($output);

另请注意,您需要 echoreturn 您的 json_encode($output);