2 ways to check if a Date is Today’s Date in laravel

January 24, 2023
Laravel
2 ways to check if a Date is Today’s Date in laravel

In this tutorial, we will be discussing how you can check if a Date is Today’s Date in laravel by two different methods. We will be using isToday method and isSameDay method.

Let’s get started!

First Method: Using IsToday()

So in this first method, we will be using IsToday() method which is present in the Carbon Library.

Here is how you do it in the controller or view:

use Carbon\Carbon;

$date = Carbon::parse('2023-01-24');

if ($date->isToday()) {
    echo "yes, its today date"
} else {
    echo "no, its not today date"
}

Second Method: Using isSameDay()

In this second method, we will be using isSameDay() method which is again present in the Carbon Library.

Here is how you do it in the controller or view:

use Carbon\Carbon;

$date = Carbon::parse('2023-01-24');

if ($date->isSameDay(Carbon::now())) {
    echo "yes, its today date"
} else {
    echo "no, its not today date"
}

Conclusion

In the article above, we have covered 2 different ways of checking if a Date is Today’s Date in laravel. See you in the next one 🙂

Tags

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon