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, 2022September 2, 2022

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

Share this:

  • Facebook
  • X

Related

Php Laravel Laravel 9 datetimelaravel

Post navigation

Previous post
Next post

Related Posts

Php Facebook Login or Signup in laravel 8 9

How to Implement Facebook Login or Signup in laravel 8 / 9 ?

March 19, 2022March 19, 2022

Facebook login or signup in laravel are demanding from start as it gives user to access the website in single click. Facebook is widely used social media platform therefore most of the users are on it and we can take leverage of it by providing the feature of login and…

Share this:

  • Facebook
  • X
Read More
Php How to Show Success and Error Flash Message in Laravel

How to Show Success and Error Flash Message in Laravel 10 ?

March 26, 2023November 4, 2023

This article aims to teach how to display flash messages indicating success or error in Laravel 10. Flash messages come in handy when notifying users of recent activities such as form submissions or website actions. In such cases, multiple types of messages may be required. Sometimes, error messages need to…

Share this:

  • Facebook
  • X
Read More

Laravel array validation with example

December 15, 2021November 12, 2023

In this tutorial i will explain to use validating a array inputs using Laravel validation library. Laravel also supports to validate group of array and we can validate a array same as normal input. In our recent article Laravel form validation how to validate a form and files. In this…

Share this:

  • Facebook
  • X
Read More

Leave a ReplyCancel reply

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 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • 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

  • How to Call Controller Method from Another Controller in Laravel ?
  • How to Get Domain Name in Laravel ?
  • How to Append Query String to Route in Laravel ?
  • How to Append URL Query Params to Pagination Laravel ?
  • How to Get Today Records in Laravel ?
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version