Search code examples
laraveldatecomparephp-carbon

How to compare carbon date in laravel?


I try to check if today is the 3 days after the registration day or not, so i compare today date with the registration date plus 3 days. But i think my code is not working, this is my code:

$get_tanggal_permohonan = DB::table('data_pemohon')->select('tanggal_permohonan')->where('noper', $noper)->first();
$tanggal_permohonan     = $get_tanggal_permohonan->tanggal_permohonan;
$Dday       = \Carbon\Carbon::parse($tanggal_permohonan);
$today      = \Carbon\Carbon::now()->toDateString();
$today      = \Carbon\Carbon::parse($date);

if($today < $Dday->subDays(3)){
    echo "not the time to come";
}else{
    echo "time to come"
}

I have no idea to solve this error, help me please. Thank you.


Solution

  • You can use DiffInDays()

    if( $Dday->diffInDays($today) > 3){
        echo "not the time to come";
    }else{
        echo "time to come"
    }