Laravel Carbon Get Current Date Time Example

Aug 19, 2021 . Admin



Hi Dev,

In This example, I will explain to you how to get the current date-time in laravel using carbon in laravel so it can easy to use in laravel app.

Here, I will give you a full example of how to laravel carbon current timestamp, laravel carbon current date, laravel carbon gets current year, laravel carbon get a current day, laravel carbon today date format, carbon current date-time laravel. so follow my example code and you can use your laravel application to copy that code.

Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Current Date Time:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon current date-time...
     *
     * @return string
    */
    public function index()
    {
        $todayDate = Carbon::now();
        dd($todayDate);
    }

}
Output :
2021-08-19 04:11:54.795338 UTC (+00:00)
Current Date:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon current date...
     *
     * @return string
    */
    public function index()
    {
        $todayDate = Carbon::now()->format('Y-m-d');
        dd($todayDate);
    }

}
Output :
"2021-08-19"
Current Time:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon current time..
     *
     * @return string
    */
    public function index()
    {
        $todayDate = Carbon::now()->format('H:i:m');
        dd($todayDate);
    }

}
Output :
"04:16:08"
Current Day/Month/Year:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel current day/month/year...
     *
     * @return string
    */
    public function index()
    {
        $day = Carbon::now()->format('d');
        $month = Carbon::now()->format('m');
        $year = Carbon::now()->format('Y');
    }

}
Output :
"19"
"08"
"2021"

It Will Help You..

#PHP #Laravel 8 #Laravel