Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carbon parse date format

I'm trying to parse a date formated like :

2017-09-20T10:59:10.0000000 01:00

I'm using Carbon, so i tried :

Carbon::createFromFormat('Y-m-dTH:i:s.u vP', $date)

Which output :

The timezone could not be found in the database\n
Unexpected data found.\n
Data missing

I guess the last timezone argument maybe wrong but i couldn't find how to parse that date format :/

Thanks for help !

like image 743
lovis91 Avatar asked Feb 27 '18 10:02

lovis91


People also ask

How do I change the date format in Carbon?

Carbon::parse($client->db)->format('d/m/y'); is probably what you're wanting. (parse the yyyy-mm-dd date in the database as a date, and convert it to d/m/y format).

How do you convert a string to a Carbon date?

$date = Carbon\Carbon::parse($rawDate); well thats it. You'll now have a Carbon instance & you can format the date as you like using Carbon helper functions.


1 Answers

You'll need to add a sign to the timezone, for example:

+01:00

Then this will work for you:

Carbon::createFromFormat('Y-m-d\TH:i:s.0000000 P', $date)

If your string can have -01:00 but instead of +01:00 you're getting 01:00, do this first:

$timezone = str_after($date, ' ');
if ($timezone[0] !== '-') {
    $date = str_before($date, ' ') . ' +' . $timezone;
}
like image 197
Alexey Mezenin Avatar answered Oct 04 '22 11:10

Alexey Mezenin