Reputation: 5195
This code:
Carbon::createFromFormat('Y-W', '2018-11');
throws this error:
The format separator does not match Trailing data
Why does this happen? The string clearly matches with the format, and it works when I use different formats and strings like 'Y-m-d' and '2018-11-11'
. What is wrong with using the week number?
Upvotes: 1
Views: 1219
Reputation: 5131
This usage is impossible in PHP (not related to Carbon):
var_dump(\DateTime::createFromFormat('Y-W', '2018-11'));
var_dump(\DateTime::getLastErrors());
Output:
bool(false)
array(4) {
["warning_count"]=>
int(0)
["warnings"]=>
array(0) {
}
["error_count"]=>
int(2)
["errors"]=>
array(2) {
[5]=>
string(35) "The format separator does not match"
[6]=>
string(13) "Trailing data"
}
}
apokryfos's suggestion is good. But be careful, the year-week numbers have different meaning depending on cultures (different start of week, start of year). Carbon handle it with ->locale()
method.
Upvotes: 2
Reputation: 40690
Since you're using carbon and given that you can't use "W" in the format you can do:
$date = Carbon::create(2018)->week(11);
Yields date:
2018-03-12 00:00:00.0 UTC (+00:00)
Upvotes: 1