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 Show Validation Error Message

How to Show Validation Error Message in Laravel ?

November 10, 2023March 16, 2024

In this article i will explain you to show validation error message in laravel. One crucial aspect of web development is handling form validation and displaying error messages to users effectively. In this guide, we will delve into the intricacies of showing validation error messages in Laravel, ensuring a smooth…

Read More
Php Laravel 10 Image Upload with Preview Example

Laravel 10 Image Upload with Preview Example

March 7, 2023March 16, 2024

In this blog post we will learn laravel image upload. Laravel 10 offers robust functionality for uploading and processing images, which is often a basic requirement for websites, ranging from setting up a profile picture to providing necessary documents. With Laravel 10, image uploads can be handled with enhanced security…

Read More
Php How to get random rows in laravel example

How to get random rows in laravel example ?

October 10, 2022March 16, 2024

In this blog post, we’ll take a look at how to get random rows in Laravel database. We’ll explore the use of the QueryBuilder, Eloquent, and the DB facade. laravel has inbuilt eloquent method to get the random records from database using the inRandomOrder method. we will understand the random…

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

  • August 2025
  • 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

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version