carbon()

carbon()

Twig Function

The carbon() function can be used to handle dates and times using Twig, prepared with a Carbon object (opens new window) and all its available functions.

The supplied value will be automatically converted to the current timezone depending on the cms.timezone configuration setting, which can be set using a site definition.

To output the current date time value:

___TWIG0___

To specify a custom value:

{{ carbon('2024-01-01 02:13:23') }}

# format

The format method can be used to apply various formats.

Meeting starts at ___TWIG0___ in Johannesburg.

# formatLocalized

Format a local time/date according to locale settings, this is the equivalent to strftime in PHP.

___TWIG0___

# diffForHumans

The diffForHumans method will render the difference between a date and now in human readible text.

___TWIG0___

# Cache Busting URLs

You can use the format to produce cache busting URLs.

// Outputs: 10.26.22.22.53.31
carbon('now').format('m.d.y.H.i.s')

Then build the URL like the following.

<img src="___TWIG0___?___TWIG1___" alt="" />

# Date Format Cheat Sheet

The following values are available when formatting dates and times. You can use these codes with the format method.

Day FormatExample
d01 through 31
DMon through Sun
j1 through 31
lSunday through Saturday
N1 (Monday) to 7 (Sunday)
Sst, nd, rd, th
w0 (Sunday) through 6 (Saturday)
z0 through 365
Week FormatExample
W42 (42nd week in year)
Month FormatExample
FJanuary through December
m01 through 12
MJan through Dec
n1 through 12
t28 through 31
Year FormatExample
Y1985, 1991, 2012, 2014, ...
y85, 91, 12, 14, ...
oSame as Y, except based on week ending
L1 (leap year), 0 otherwise
Time FormatExample
aam or pm
AAM or PM
B000 through 999
g1 through 12
G0 through 23
h01 through 12
H01 through 23
i00 through 59
u123456 (microseconds)
Timezone FormatExample
eUTC, GMT, Atlantic/Azores
I1 (daylight), 0 otherwise
O+0200
P+02:00
TEST, MDT, ...
Z-43200 through 50400 (timezone offset)
Date & Time FormatExample
c2004-02-12T15:19:21+00:00
rThu, 21 Dec 2000 16:01:07 +0200
USeconds since Jan 1 1970 00:00:00 GMT