How to get current date time using Laravel
In this tutorial, we will learn How to get the current date time using Laravel. We use Laravel carbon to get the current date-time. I would like to share the Laravel carbon current timestamp. In this tutorial, you will learn Laravel carbon current date. And if you want to see an example of how to get the current date time using Laravel then you are in the right place. In this tutorial, We creating a basic example of Laravel carbon to get the current day.
Table of Contents
- Get Current Date
- Get Current Time
- Get Current Date Time
- Get Current Day/Month/Year
Get Current Date
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Carbon\Carbon; class HomeController extends Controller { public function index() { $date = Carbon::now()->format('Y-m-d'); dd($date); } } Output: "2021-09-06"
Get Current Time
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Carbon\Carbon; class HomeController extends Controller { public function index() { $time = Carbon::now()->format('H:i:m'); dd($time); } } Output: "16:32:09"
Get Current Date Time
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Carbon\Carbon; class HomeController extends Controller { public function index() { $datetime = Carbon::now(); dd($datetime); } } Output: date: 2021-09-06 16:32:33.176270 UTC (+00:00)
Get Current Day/Month/Year
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Carbon\Carbon; class HomeController extends Controller { public function index() { $day = Carbon::now()->format('d'); $month = Carbon::now()->format('m'); $year = Carbon::now()->format('Y'); echo $day; echo '<br>'; echo $month; echo '<br>'; echo $year; } } Output: 06 09 2021