laravelphp-carbon

Why comparing 2 dates I got invalid results?


On laravel 10 site

I need to mark all tasks which are greater/equal today and less 3 days:

I do it with code :

        dump(  $today);    // 2024-07-15 00:00:00.0 UTC (+00:00)
        dump( '$task->deadline_at');
        dump( $task->deadline_at); // 2024-08-03 00:00:00.0 UTC (+00:00)

        dump( '$task->deadline_at->gte($today)::');
        dump( $task->deadline_at->gte($today)); // outputed true - yes as expected

        dump( '$task->deadline_at->lt($today->addDays(3))::');
        dump( $task->deadline_at->lt($today->addDays(3))); // outputed false - yes as expected

        $less3days = ($task->deadline_at->gte($today)) and ($task->deadline_at->lt($today->addDays(3)));
        dump($task->id);
        dd($less3days); // But final result is TRUE - can not understand why - as I have "(true) and (false)" ?

Where is an error ?


Solution

  • Try changing that one line to be this:

    $less3days = ($task->deadline_at->gte($today) && $task->deadline_at->lt($today->addDays(3)));