Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Get Current Date, Time and Day in Laravel

How to Get Current Date Time and Format in Laravel ?

Aman Jain, September 2, 2022March 16, 2024

Laravel has carbon library to get the current date time and format in laravel and also we can use core php date functions to fetch the date and time. After php 5, php also introduced DateTime class to conduct the operations of date time related functionality. Sometimes in our application we want to use current date, time and day to compare the past or future date or to show specific time so we can either use carbon library or core php DateTime class.

This example will work in all version of laravel including laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9. In example we will show date and time with Carbon, DateTime class and core php date() function.

Also Read : How to Change Date Format in Laravel ?

Let’s begin the tutorial of Get Current Date Time and format in Laravel So to print current date time in laravel we can use as follow

Method 1 : Using Carbon to get Date Time

In this method we will use carbon class as follow

$dattime = \Carbon\Carbon::now();
echo $dattime->toDateTimeString();

this will output the date in format Y-m-d H:i:s so the output will be

2022-09-02 02:23:19

Another way to get the information is as follow

$date = \Carbon\Carbon::now();
return $date->toArray();

And output

{
  "year": 2022,
  "month": 9,
  "day": 2,
  "dayOfWeek": 5,
  "dayOfYear": 245,
  "hour": 2,
  "minute": 26,
  "second": 16,
  "micro": 505774,
  "timestamp": 1662085576,
  "formatted": "2022-09-02 02:26:16",
  "timezone": {
    "timezone_type": 3,
    "timezone": "UTC"
  }
}

Method 2 : Using date function to get Date Time

In this method we will use date() core function class as follow

$dattime = date("Y-m-d H:i:s");
echo $dattime;

this will output the date in format Y-m-d H:i:s and we can change in any date format so the output will be

2022-09-02 02:23:19

Method 3 : Using DateTime class to get Date Time

In this method we will use DateTime() class class as follow

$datetime = new DateTime();
echo $datetime->format('Y-m-d H:i:s');

this will output the date in format Y-m-d H:i:s and we can change in any date format so the output will be

2022-09-02 02:23:19

Related

Php Laravel Laravel 9 datetimelaravel

Post navigation

Previous post
Next post

Related Posts

Php Laravel custom login and register

Laravel 8 Custom login and registration with example

December 3, 2021December 3, 2021

In Laravel 8 there is multiple ways to implement the login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can say we…

Read More
Php How to Use Broadcast to Send Web Socket Event in Laravel

How to Use Broadcast to Send Web Socket Event in Laravel ?

June 23, 2022June 26, 2022

Broadcasting is used to send real time message to client. In this article we will learn to use broadcast to send web socket event in laravel. Web sockets are used to communicate with client from server for real time communication. Broadcasting very useful to send update to client when any…

Read More
Php Creating resource controller in laravel

How to create resource controller in Laravel 8 ?

November 7, 2021November 7, 2021

Resource controller is helpful when you want Create, Read, Update and Delete (CRUD) operations. If you have a model with same set of actions like crud then resource controller are useful. we can create resource controller easily using the artisan command. Above command will generate controller ImageController in \app\Http\Controllers. As…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version