Reputation: 1344
I'm experiencing a weird problem.
I'm using Carbon for dates. I want to use the format Y-W (year,week) which is working correctly. Here i store it into the DB:
$weekDate = Carbon::createFromFormat('d-m-y', "{$key}")->format('Y-W');
DB::table('backorder_voorspelling')->insert([
'artikelcode' => $articlecode,
'week' => $weekDate,
'aantal' => $value,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
]);
The database record is correct:
{#426 ▼
+"id": 1
+"artikelcode": "articlecode"
+"week": "2017-44"
+"aantal": "6"
+"created_at": "2018-01-18 11:46:45"
+"updated_at": "2018-01-18 11:46:45"
}
Later on i want to convert the Y-W back to a carbon time and this is telling me:
The code i use to create the carbon time:
$startOfWeek = Carbon::createFromFormat('Y-W', $row->week);
The formats are the same, when storing i use the format ('Y-W') and when creatingFromFormat i use the format ('Y-W'), yet it's not working...
I tried replacing the - for / but this returns the same error.
Any help is appreciated.
Upvotes: 0
Views: 1248
Reputation: 17417
Not all date format characters can be used in DateTime::createFromFormat
(which is what Carbon extends). Unfortunately for you, W
is one of the ones that's missing.
From the manual:
The format that the passed in string should be in. See the formatting options below. In most cases, the same letters as for the date() can be used.
You can work round this by manually calling setISODate
on a new DateTime (or Carbon) instance:
list ($year, $week) = explode('-', '2017-44');
$d = new DateTime;
$d->setISODate($year, $week);
setISODate
also accepts a third $day
parameter - by default it will set to the first day of the week, which I think is what you want.
Upvotes: 2