Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vipin93's avatar
Level 13

what is the best way to compare date?

I try to compare date with up to today, so problem is that how can i use with carbon like i want

$attendance = Attendance::where('teacher_id',$teachrID)->whereDate('date',previous_date_upto_today)->first();

thnaks

0 likes
5 replies
samalapsy's avatar

The best I've tried is to convert both date to milliseconds using the PHP strtotime("{date}") then compare both dates.

Example

$date1 = strtotime(date("d-m-y"));
$date2 = strtotime(date("d-m-y"));

if ($date2 >= $date1){
    echo 'Date 2 is greater than Date 1';
}else{
    echo 'Date 1 is greater than Date 2';
}

I hope it's helpful?

vipin93's avatar
vipin93
OP
Best Answer
Level 13

sorry for this questioned silly questioned i have to use "<=" like

$attendance = Attendance::
                           where('teacher_id', $teachrID)
                           ->whereDate('date' ,'<=' , $today)->first();  
Snapey's avatar

@jlrdw carbon comparisons are not relevant in a sql query

@vipin93 does not explain where $today comes from?

I see you have marked it answered so I suppose you got your solution?

vipin93's avatar
Level 13

@Snapey yes I got solution

$today = Carbon::today()->format('Y-m-d');

Please or to participate in this conversation.