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

Crazylife's avatar

How to compare a string with carbon date if more than or less than?

Is it correct to compare between string date and carbon date?

$today = Carbon::today();
$date= '22-7-2017';

if($date > $today){
//.....
}
0 likes
4 replies
hajrovica's avatar

No, you need to instantiate $date as Carbon object, and then you can compare 2 values

Crazylife's avatar
$date1 =Carbon::parse($date);
$today = Carbon::now();
flyingearl's avatar

I wouldn't suggest it as you are likely to get false positives.

I would parse your string date and make it a Carbon object also and then do the compare.

So:

$date = Carbon::parse('22-7-2017');

Hope that helps.

1 like

Please or to participate in this conversation.