How to Check Date is Today’s Date or not in Laravel Carbon?

Hello dev, today we are going to learn How to Check Date is Today’s Date or not in Laravel Carbon? This tutorial will cover on tutorial to check data is today date or not with carbon in laravel application.

This extensive guide will teach you laravel carbon check if date is today. let’s discuss about how to check date is today date in laravel. you will learn how to compare date with current date in laravel. This example will help you how to check if date is today date in laravel.

You can use these tips with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

Laravel Carbon provides isToday() method to check date is today date or not. so let’s see the simple example below:

Also Read: Laravel Pagination Tutorial

Example to Check Date is Today’s Date or not

Example:

app/Http/Controllers/DemoController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use Carbon\Carbon;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        /* Laravel Carbon Check Date is Today */
        $user = User::find(10);
        if ($user->created_at->isToday()){
            print("User Created Today.");
        }
  
        /* Laravel Carbon Check Date is Today 2 */
        $date = Carbon::parse("2022-10-27");
        if ($date->isToday()){
            print("Date is Today.");
        }
  
    }
}

Output:

User Created Today.
Date is Today.

Conclusion

Today, We had learn How to Check Date is Today’s Date or not in Laravel Carbon?. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: How to Get All env Variables in Laravel?

3 Comments

Leave a Reply