Carbon Parse to Iso8601

I am trying to get the current time and format it like:

"2018-09-26T21:40:29+02:00"

But when I try:

$isoDate = \Carbon\Carbon::now()->format('c');

as I understood passing a c to format function will parse it to iso8601 but clearly thats not the case.

Any help on how to parse current time to ISO8601 OR 20181001T094006Z

14

3 Answers

echo Carbon::now()->toIso8601String();
Carbon::now()->toISOString()

this will return "2020-05-12T13:13:42.817684Z"

Carbon::now()->toIso8601String()

this will return "2020-05-12T13:15:32+00:00"

using corePHP

date(DateTime::ATOM, time())

this will return "2021-08-22T15:26:48+10:00"

There is no single 8601 format. 8601 defines various acceptable formats, of which PHP's c represents one of the most common forms.

There is no single character for the specific 8601 format you wish but a format of Ymd\THis\Z should work. T and Zare literal, so escape them with a backslash to avoid them being interpreted in the format string. Be sure that only UTC timestamps are used with this particular format.

lists all the acceptable format characters.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest