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 orderBy clause in eloquent with example

Laravel orderBy clause in eloquent with example

October 5, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel orderBy clause in the Eloquent ORM.Laravel provides an expressive, fluent interface for creating and retrieving records in your database. When we work with databases in Laravel, we often need to order the results we get…

Read More
Php install laravel composer

How to install laravel 8 ?

September 20, 2021May 6, 2022

Laravel is most popular framework of PHP. It provides lots of in-build tools and library to build the web application. Laravel is used to to create robust custom web applications with ease of amazing developer experience. Laravel is easily customizable and provides tools like ORM, Validation libraries, Queue, Mail, Routing,…

Read More
Php make a component in laravel 10

How to create component in Laravel 11 ?

July 14, 2024July 14, 2024

In this tutorial i will show you simple html based component in Laravel 11 using blade.Creating reusable components is essential in contemporary web development, enabling developers to streamline code and enhance efficiency. Laravel 11, a leading PHP framework, offers a robust component-building system. Similar to Angular or React, Laravel provides…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version