How to Get the Current Date Time and Day in Laravel?

Last updated on : Jul 7, 2023 by Ashwini Upadhyay

In this tutorial, We will learn How to Get the Current Date Time, and Day in Laravel. In this post, we will see how can get the current date, time, and day in Laravel with different formats using the Carbon library we’ll also look at some commonly used format attributes and their corresponding outputs.

Let’s see how you can do it:

Laravel Get Current Date Time and Day using Carbon :

Make sure you have Carbon installed in your Laravel project. If you don’t have it installed, you can install it using Composer by running the following command in your terminal:

composer require nesbot/carbon

Once Carbon is installed, you can use it to get the current date, time, and day in your Laravel application.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DayDateTimeController extends Controller
{
    public function index()
    {
        // Get the current date
        $currentDate = Carbon::now()->toDateString();
        echo $currentDate; // Output: 2023-07-05

        // Get the current time
        $currentTime = Carbon::now()->toTimeString();
        echo $currentTime; // Output: 12:34:56

        // Get the current day
        $currentDay = Carbon::now()->format('l');
        echo $currentDay; // Output: Wednesday
    }
}

In the format() method, you can use various format characters to define the format you desire. Here are some commonly used format characters:

List of format attributes supported by the Carbon library in Laravel

As you can see Here’s a comprehensive list of format attributes supported by the Carbon library in Laravel, along with examples of their usage:

✧ Current date and time

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    public function index()
    {
        // Get the current date and time
        $date = Carbon::now();
        echo $date; // Output: // The current date and time is 2023-07-05 12:34:56
    }
}

✧ Year

echo $date->format('Y');     // Output: 2023 (4-digit year)
echo $date->format('y');     // Output: 23 (2-digit year)
  • Y: A four-digit representation of the year (e.g., 2023).
  • y: A two-digit representation of the year (e.g., 23).

✧ Month

echo $date->format('F');     // Output: July (full month name)
echo $date->format('M');     // Output: Jul (short month name)
echo $date->format('m');     // Output: 07 (2-digit month number)
echo $date->format('n');     // Output: 7 (month number without leading zeros)
  • F: The full textual representation of the month (e.g., July).
  • M: A short textual representation of the month (e.g., Jul).
  • m: Numeric representation of a month with leading zeros (e.g., 07).
  • n: Numeric representation of a month without leading zeros (e.g., 7).

✧ Day

echo $date->format('d');     // Output: 05 (day of the month with leading zeros)
echo $date->format('j');     // Output: 5 (day of the month without leading zeros)
echo $date->format('D');     // Output: Wed (short day name)
echo $date->format('l');     // Output: Wednesday (full day name)
  • d: Day of the month, numeric representation with leading zeros (e.g., 05).
  • j: Day of the month without leading zeros (e.g., 5).
  • D: A textual representation of a day, three letters (e.g., Wed).
  • l: A full textual representation of the day (e.g., Wednesday).

✧ Time

echo $date->format('H');     // Output: 12 (24-hour format)
echo $date->format('h');     // Output: 12 (12-hour format)
echo $date->format('i');     // Output: 34 (minutes)
echo $date->format('s');     // Output: 56 (seconds)
echo $date->format('A');     // Output: PM (uppercase AM/PM)
echo $date->format('a');     // Output: pm (lowercase am/pm)
  • H: 24-hour format of an hour with leading zeros (e.g., 12).
  • h: 12-hour format of an hour with leading zeros (e.g., 12).
  • i: Minutes with leading zeros (e.g., 34).
  • s: Seconds with leading zeros (e.g., 56).
  • A: Uppercase AM or PM (e.g., PM).
  • a: Lowercase am or pm (e.g., pm).

✧ Other Attributes

echo $date->format('w');     // Output: 3 (numeric representation of the day of the week, starting from 0 for Sunday)
echo $date->format('z');     // Output: 185 (day of the year, starting from 0)
echo $date->format('N');     // Output: 3 (ISO-8601 numeric representation of the day of the week, starting from 1 for Monday)
echo $date->format('W');     // Output: 27 (ISO-8601 week number of year)
echo $date->format('t');     // Output: 31 (number of days in the given month)
  • w: Numeric representation of the day of the week, starting from 0 (e.g., 3 for Wednesday).
  • z: The day of the year starting from 0 (e.g., 185).
  • N: ISO-8601 numeric representation of the day of the week, starting from 1 (e.g., 3 for Wednesday).
  • W: ISO-8601 week number of the year (e.g., 27).
  • t: Number of days in the given month (e.g., 31).

✧ Timezone

echo $date->format('e');     // Output: UTC (timezone identifier)
echo $date->format('P');     // Output: +00:00 (difference to UTC in hour:minute format)
echo $date->format('O');     // Output: +0000 (difference to UTC in hourminute format)
  • e: Timezone identifier (e.g., UTC).
  • P: Difference to UTC in hour:minute format (e.g., +00:00).
  • O: Difference to UTC in hourminute format (e.g., +0000).

✧ Complete the Date and Time

echo $date->format('Y-m-d H:i:s');  // Output: 2023-07-05 12:34:56
echo $date->toDateTimeString();     // Output: 2023-07-05 12:34:56
  • Y-m-d H:i:s: Year-month-day hour:minute: second format (e.g., 2023-07-05 12:34:56).
  • toDateTimeString(): Complete the date and time in the default format.

✧ Relative Formats

echo $date->diffForHumans();        // Output: 1 second ago (depending on the current time)
  • diffForHumans(): Provides a human-readable representation of the date relative to the current time (e.g., 1 second ago).

These are some of the most commonly used format attributes in Carbon. You can combine them as needed to achieve the desired date and time formatting in your Laravel application. Additionally, you can refer to the official Carbon documentation for more information on available formatting options and customization possibilities: Carbon Docs

ashwini upadhyay

Ashwini Upadhyay

I'm a full-stack developer, owner of LogicalDuniya.I love to write tutorials and tips that can help to other artisan. I love to write on PHP, Laravel, Angular, Vue, React, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.

Recommended Posts :


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Press ESC to close