Reputation: 577
I have a json with date of users. Dates can be format:
"bdate": "25.10",
"bdate": "8.7.1990"
"bdate": "13.10.1984"
"bdate": "7.3"
How I can parse these dates in carbon globally?
When I use:
Carbon::parse($people->bdate)
I get error:
DateTime::__construct(): Failed to parse time string (25.10) at position 0 (2): Unexpected character
Upvotes: 1
Views: 4451
Reputation: 9853
This could be another work around of your problem.
function getBirthDateInCarbon($date){
$count = substr_count($date,'.');
if($count==1){
return \Carbon\Carbon::createFromFormat('d.m', $date);//default year will be current year
}
return \Carbon\Carbon::createFromFormat('d.m.Y', $date);
}
Upvotes: 2
Reputation: 1848
There is probably a more simple solution, tested this and it works
$date = [null, null, null];
$data = explode('.', "25.10");
foreach ($data as $key => $da) {
$date[$key] = $da;
}
$date = Carbon::createFromDate($date[2], $date[1], $date[0]);
Upvotes: 1