DateTime Formats
This article is everything you need to know about date/time in Laravel.
We will cover important things like immutability, formatting, exending Carbon with macros as well as helpful tips like a format cheat sheet, youtube duration formats and constants.
Cheat Sheet
Formats
Day Format | Value/Example |
---|---|
d | 01 through 31 |
D | Mon through Sun |
j | 1 through 31 |
l (lowercase ‘L’) | Sunday throguh Saturday |
N | 1 (Monday) to 7 (Sunday) |
S | st, nd, rd, th |
w | 0 (sunday) through 6 (Saturday) |
z | 0 through 365 |
Week Format | Value/Example |
---|---|
W | 42 (42nd week in year) |
Month Format | Value/Example |
---|---|
F | January through December |
m | 01 through 12 |
M | Jan through Dec |
n | 1 through 12 |
t | 28 through 31 |
Year Format | Value/Example |
---|---|
L | 1 (leap year), 0 otherwise |
o | same as Y, except that it use week number to decide which year it falls onto |
Y | 1991, 2012, 2014, … |
y | 91, 12, 14, … |
Time Format | Value/Example |
---|---|
a | am or pm |
A | AM or PM |
B | 000 through 999 |
g | 1 through 12 |
G | 0 through 23 |
h | 01 through 12 |
H | 01 through 23 |
i | 00 through 59 |
u | 123456 (microseconds) |
Timezone Format | Value/Example | Description |
---|---|---|
e | UTC, GMT, Atlantic/Azores | Timezone identifier |
T | EST, BST, … | Timezone abbreviation (if known) or the GMT offset. |
I (capital i) | 1 (daylight), 0 otherwise | Is date in daylight saving time |
O | +0200 | Difference to Greenwich time (GMT) without colon between hours and minutes |
P | +02:00 | The same as O with colon |
Z | -43200 through 50400 (timezone offset) | Timezone offset in seconds. West UTC = negative, East UTC = positive |
Date/Time Format | Value/Example | Description |
---|---|---|
c | 2004-02-12T15:19:21+00:00 | ISO 8601 date |
r | Thu, 21 Dec 2000 16:01:07 +0200 | RFC 2822 formatted date |
U | 1617265800 | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) |
Examples
Here are some common formats using 2021-04-01 09:30:00
Format | Code | Example |
---|---|---|
RFC2822 | DateTime::RFC2822 | Thu, 01 Apr 2021 09:30:00 +0000 |
ISO | c | 2021-04-01T09:30:00+00:00 |
ISO Date | Y-m-d | 2021-04-01 |
ISO Week | Y-\WW | 2021-W33 |
ISO Weekday | Y-\WW-N | 2021-W33-2 |
Ordinal (Year-Day) | Y-z | 2021-90 |
RFC7231 | Carbon::RFC7231_FORMAT | Thu, 01 Apr 2021 09:30:00 GMT |
RFC7231 | D, d M Y H:i:s \G\M\T | Thu, 01 Apr 2021 09:30:00 GMT |
UK (short) | d/m/y | 01/04/21 |
UK (medium) | j M Y | 1 Apr 2021 |
UK (long) | d F Y | 01 April 2021 |
Letter | F d, Y | April 01, 2021 |
12 Hour | g:i a | 9:30 am |
24 Hour | H:i | 09:30 |
Military | Hi | 0930 |
Words | l, jS F Y \a\t g:i A (T) | Thursday, 1st April 2021 at 9:30 AM (UTC) |
Carbon constants
Carbon comes with a number of constants you can to avoid ‘magic’ dates/times.
Custom date formats
Carbon already comes with a number of different conversion methods in Carbon\Traits\Converter.php
. Here’s a few:
toDateString
toCookieString
toRfc822String
toRfc7231String
toArray
toIsoString
Extending Carbon
Using custom format constants might be useful if you use a number of different formats throughout your application, for example a finance system generating customer invoices, sending reports, working with accounting periods etc.
But if you usually use the same format throughout your application you might want to keep it at your fingertips.
We could use the Carbon’s converter ‘method’ approach for our own common date formats listed in the examples section.
You can extend Illuminate\Support\Carbon
in a service provider with macros.
Did you know you can create and register your own service providers!
Custom constants
If you have a number of different formats or you just want to clean up your service provider’s macro calls you could set the string formats as enum or class constants.
Here we are using Enums (PHP 8.1+)
but you could always use class constants
or in a macro:
Blade stringables
You could set the default blade carbon output format by changing the stringable implementation in a service provider.
Immutable dates by default
Immutable dates prevent modifications to an instance following the original instance.
Making dates immutable is sort of like copying a version the previous instance before making any changes.
We can force Laravel to use immutable dates by default by calling the Date
facade in the boot method of a service provider.
Now any eloquent model datetime attributes will be immutable by default 🎉
Just remember to use Date instead of Carbon when instantiating dates manually!
This means you no longer have to remember to use the CarbonImmutable
class, or cast your models:
Creating Dates
Duration
Simple Duration string
Youtube Duration (Display format)
Simple
Warning! This will not work over 24 hours.
Advanced
This will allow you to pass $seconds
as an integer or a CarbonInterval
object.
CarbonInterval Macro
CarbonInterval Mixin
Create a mixin class and register to the framework via a service provider.
CarbonInterval Mixin (Trait)
Create Time From Format
Magic Time
Fluent Interval
Setting Time
Convert DateTime to Carbon
Periods
Generate an array of dates for a the next 30 days: