Carbon parse to ISO8601
Asked Answered
P

3

30

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

Pittsburgh answered 26/9, 2018 at 19:43 Comment(14)
There's no single 8601 format. Construct it yourself with the date formats.Fell
@Devon wikipedia.org/wiki/ISO_8601Pittsburgh
What happens when you try what you have? c should translate to ISO8601. Here's what I get when I run echo \Carbon\Carbon::now()->format("c");: 2018-09-26T20:05:30+00:00Souterrain
@AladinSmall, yes, and there are about 8 different formats there...Fell
@Devon i think I am very constructive on my question, however due to my poor reputation I am a good catch for trolling. So please read the question again its specified what kind of iso format : ISO8601 OR 20181001T094006ZPittsburgh
Did you atleast try to read the format manual and construct it yourself using existing characters like YmdTHisZFell
@Souterrain I did tried but I get this : 2018-09-26T21:40:29+02:00 which is not acceptable from the web service that I am trying to sent, as they want smth like: 20181001T094006Z . I mean I know the ugly way of parsing it that way by extracting year month .... and concat but It must be smth that I am missing.Pittsburgh
That's exactly what you're looking for, according to your question...Souterrain
@Devon do u know what should I pass in order to get the correct format ?Pittsburgh
What output did you get?Vesicle
@Devon that was it \Carbon\Carbon::now()->format('YmdTHisZ') I get: "20180926CEST2216517200" thats what I need to know so add the answer to mark the question as problem solved. thanks.Pittsburgh
Ok, so T and Z are format characters so escaping them with a backslash should workFell
@Devon works. thanks finally on the same frequency.Pittsburgh
Consider changing accepted answer. Nurdin's answer works.Unideaed
F
13

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.

http://php.net/manual/en/function.date.php lists all the acceptable format characters.

Fell answered 26/9, 2018 at 20:21 Comment(2)
Are you sure that this is a valid ISO 8601 format? I don't think the hyphens between year, month and date are optional neither the colons between hour, minute and seconds.Appendant
Yes. Like I said in the answer and comments, 8601 has multiple formats.Fell
P
52
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

Pay answered 12/5, 2020 at 13:17 Comment(0)
K
37
echo Carbon::now()->toIso8601String();
Komarek answered 16/5, 2019 at 5:29 Comment(0)
F
13

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.

http://php.net/manual/en/function.date.php lists all the acceptable format characters.

Fell answered 26/9, 2018 at 20:21 Comment(2)
Are you sure that this is a valid ISO 8601 format? I don't think the hyphens between year, month and date are optional neither the colons between hour, minute and seconds.Appendant
Yes. Like I said in the answer and comments, 8601 has multiple formats.Fell

© 2022 - 2024 — McMap. All rights reserved.