In Laravel, Carbon is a popular date/time library that provides an easy way to work with dates in PHP. To check if a date is in the past using Carbon in Laravel, you can use the isPast() method.
Here’s an example code snippet:
use Carbon\Carbon;
$date = Carbon::parse('2022-01-01');
if ($date->isPast()) {
echo 'The date is in the past.';
} else {
echo 'The date is in the future.';
}
In this example, we’re starting with a date string of ‘2022-01-01’, which is parsed into a Carbon object using the static parse() method. This creates a new Carbon instance representing the given date.
The isPast() method is then called on the Carbon object, which returns a boolean value indicating whether the date is in the past (true) or not (false).
Finally, an if/else statement is used to output a message depending on whether the date is in the past or not.
If the date is in the past, the output will be ‘The date is in the past.’ Otherwise, the output will be ‘The date is in the future.’