PASSWORD RESET

Your destination for complete Tech news

How to check if a date is in past in Laravel Carbon?

2.53K 0
< 1 min read

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.’

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.