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

Time:11-09

I want to compare the Start Day of the current date with the Start Day of a custom date, retrieved from the DB (which is a UNIX timestamp).

So I coded this at the Controller:

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

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

So I tested this with three different dates. And dd() results of each one goes like this:

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

And the result of dd(now()->startOfDay()); is date: 2021-11-09 00:00:00.0 Asia/Tehran ( 03:30). Which means only the $popup[1]->datep is capable of showing.

But now the problem is, it shows the 3rd date as well! However, the Start Day of it is equals to 10 while the current Start Day is 11.

So what's going wrong here?

How can I properly compare the Start Day of the current date with the Start Day of retrieved data?


Full Code:

    $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);

Then at the Blade, I tried showing $output as Sweet Alert pop message.

CodePudding user response:

You need to "reset" the status of $result at the beginning of each iteration:

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
    }
}

Once $popup[1]->datep is hit $result is set to true no matter your condition - that's why $popup[2]->datep also shows up.

Edit: I'd get rid of $result and do the "calculations" right in your date check:

<?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);

Also note that you need to echo or return your json_encode($output);

  • Related