In the world of Laravel development, managing dates and times is a crucial aspect of building robust applications. Laravel Carbon, an extension of the PHP DateTime class, offers powerful utilities for handling date and time manipulation with ease. In this comprehensive guide, we delve into the intricacies of time formatting using Laravel Carbon, covering a wide range of formats and examples.
Additionally, we dive into specific time formatting options, focusing on displaying time with AM and PM indicators. Whether you need to format time in 12-hour or 24-hour format, with or without leading zeros, we've got you covered. Our guide walks you through different time formatting styles, helping you choose the one that best suits your application's requirements.
Laravel Carbon date and time formats along with examples and code:
Default Format (Y-m-d H:i:s): This is the default format used by Carbon in Laravel.
$date = \Carbon\Carbon::now(); echo $date; // Output: 2024-02-06 10:30:00
Day-Month-Year (d-m-Y): This format displays the date in the day-month-year format.
$date = \Carbon\Carbon::now(); echo $date->format('d-m-Y'); // Output: 06-02-2024
Month-Day-Year (M d, Y): This format displays the date as Month Day, Year.
$date = \Carbon\Carbon::now(); echo $date->format('M d, Y'); // Output: Feb 06, 2024
24-hour Time Format (H:i:s): This format displays the time in 24-hour format.
$date = \Carbon\Carbon::now(); echo $date->format('H:i:s'); // Output: 10:30:00
AM/PM Time Format (h:i:s A): This format displays the time with AM/PM indicator.
$date = \Carbon\Carbon::now(); echo $date->format('h:i:s A'); // Output: 10:30:00 AM
Short Date (d/m/y): This format displays the date in the short day/month/year format.
$date = \Carbon\Carbon::now(); echo $date->format('d/m/y'); // Output: 06/02/24
Day of Week (l): This format displays the full name of the day of the week.
$date = \Carbon\Carbon::now(); echo $date->format('l'); // Output: Monday
Month and Year (F Y): This format displays the full name of the month and the year.
$date = \Carbon\Carbon::now(); echo $date->format('F Y'); // Output: February 2024
Week Number (W): This format displays the ISO-8601 week number of the year.
$date = \Carbon\Carbon::now(); echo $date->format('W'); // Output: 06
Timezone (e): This format displays the timezone identifier.
$date = \Carbon\Carbon::now(); echo $date->format('e'); // Output: UTC
ISO 8601 Format (c): This format represents the date and time in ISO 8601 format.
$date = \Carbon\Carbon::now(); echo $date->format('c'); // Output: 2024-02-06T10:30:00+00:00
Short Month and Year (M y): This format displays the short name of the month and the last two digits of the year.
$date = \Carbon\Carbon::now(); echo $date->format('M y'); // Output: Feb 24
Timestamp (U): This format represents the Unix timestamp.
$date = \Carbon\Carbon::now(); echo $date->format('U'); // Output: 1691013000
Weekday, Month, Day, Time, Year (l, F jS, Y g:i A): This format combines the weekday, full month name, day with suffix, time in AM/PM format, and year.
$date = \Carbon\Carbon::now(); echo $date->format('l, F jS, Y g:i A'); // Output: Monday, February 6th, 2024 10:30 AM
Relative Time (diffForHumans()): This method formats the date and time as a human-readable relative time.
$date = \Carbon\Carbon::now()->subDays(3); echo $date->diffForHumans(); // Output: 3 days ago
RFC 2822 Format (r): This format represents the date and time in RFC 2822 format.
$date = \Carbon\Carbon::now(); echo $date->format('r'); // Output: Mon, 06 Feb 2024 10:30:00 +0000
Year, Month, and Day (Y-m-d): This format displays the date in the year-month-day format.
$date = \Carbon\Carbon::now(); echo $date->format('Y-m-d'); // Output: 2024-02-06
Hour, Minute, Second (H:i:s): This format displays the time in the hour-minute-second format.
$date = \Carbon\Carbon::now(); echo $date->format('H:i:s'); // Output: 10:30:00
Day of Year (z): This format represents the day of the year (from 0 through 365).
$date = \Carbon\Carbon::now(); echo $date->format('z'); // Output: 36
Day of Week (N): This format represents the ISO-8601 numeric representation of the day of the week (1 for Monday, 7 for Sunday).
$date = \Carbon\Carbon::now(); echo $date->format('N'); // Output: 1 (for Monday)
AM/PM Time Format with Timezone (h:i:s A T): This format displays the time in AM/PM format with timezone.
$date = \Carbon\Carbon::now(); echo $date->format('h:i:s A T'); // Output: 10:30:00 AM UTC
Short Time with AM/PM (g:i A): This format displays the time in 12-hour format with AM/PM.
$date = \Carbon\Carbon::now(); echo $date->format('g:i A'); // Output: 10:30 AM
Hour and Minute with AM/PM (h:i A): This format displays the hour and minute with AM/PM.
$date = \Carbon\Carbon::now(); echo $date->format('h:i A'); // Output: 10:30 AM
Hour and Minute with Leading Zeros (h:ia): This format displays the hour and minute with leading zeros and AM/PM.
$date = \Carbon\Carbon::now(); echo $date->format('h:ia'); // Output: 10:30am
Lowercase AM/PM (g:ia): This format displays the time in lowercase with AM/PM.
$date = \Carbon\Carbon::now(); echo $date->format('g:ia'); // Output: 10:30am
These formats provide additional options for displaying time with AM and PM indicators in different styles and formats.
Date format in Laravel Blades
Start Date: 2 Feb 2024 10:02 AM
End Date: 2 Mar 2024 10:02 AM
{{ \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $order->stripeSubscription->start_date)->format('j M Y h:i A') }}
{{ \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $order->stripeSubscription->end_date)->format('j M Y h:i A') }}
Join Nodejs community click here