I'm encountering an unexpected outcome when attempting to compare dates in my PHP code. Here's what I'm doing:
<?php
$date = Carbon::now()->locale('fr_FR');
$date->subMonth();
$previousMonth = ucfirst($date->monthName) . ' ' . $date->year;
$currentDate = Carbon::now()->locale('fr_FR');
$currentDate->firstOfMonth();
$firstDayOfPreviousMonth = $currentDate->subMonth()->firstOfMonth(); // "2024-02-01"
$lastDayOfPreviousMonth = $firstDayOfPreviousMonth->copy()->lastOfMonth(); // "2024-02-29"
$carbonSubmissionTime = Carbon::createFromTimestamp(1709216823); // "2024-02-29"
if ($carbonSubmissionTime->gte($firstDayOfPreviousMonth) && $carbonSubmissionTime->lte($lastDayOfPreviousMonth)) {
echo "ok";
}else{
echo "nok";
}
You can test the code in: https://play.phpsandbox.io/embed/nesbot/carbon
The expectation is to receive "ok" because "2024-02-29" falls within the range of "2024-02-01" and "2024-02-29". However, the actual result is "nok".
What might be causing this discrepancy?
EDIT
I also tried using betweenIncluded but gives the same result:
if($carbonSubmissionTime->betweenIncluded($firstDayOfPreviousMonth, $lastDayOfPreviousMonth)){}
The discrepancy comes from the fact that
$carbonSubmissionTimealso has a time component to it where$lastDayOfPreviousMonthhas its time set to00:00:00.If we add the following debug statements before the comparison, the issue is apparent:
This yields:
Either change the last day of the previous month to have a time of 23:59:59, or zero out the time like so:
Making either of those changes produces the desired output.
A basic date string comparison also works: