2

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.

3 Answers3

2

You can use DiffInDays()

if( $Dday->diffInDays($today) > 3){
    echo "not the time to come";
}else{
    echo "time to come"
}
FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
  • 1
    This doesnt work as the diffInDays is an absolute value, ie it may be in 3 days time or 3 days may have elapsed and it will give the same result. – omarjebari Jun 25 '21 at 12:40
1

Question already anwsered here How to compare two Carbon Timestamps?

if (Carbon::parse($date)->gt(Carbon::now()))

for more http://carbon.nesbot.com/docs/#api-comparison

linktoahref
  • 7,812
  • 3
  • 29
  • 51
Rahul
  • 1
  • 1
  • 6
1

You can use the isSameDay() method and the Laravel today() helper function:

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

if ($Dday->addDays(3)->isSameDay(today())) {
    echo "not the time to come";
} else {
    echo "time to come";
}
Rwd
  • 34,180
  • 6
  • 64
  • 78