Reputation:
I'm extracting dates from documents. The dates are varying in format, from American to European and from dashes to backslashes.
$d1 = '03/12/2017';
$d2 = '12/03/2017';
$d3 = '26/03/2017';
Carbon::parse($d1)->toDateString(); // the month is read as March but it should be read as December
Carbon::parse($d2)->toDateString(); // the month is read as December but it should be March
Carbon::parse($d2)->toDateString(); // throws an ErrorException failing to parse the time due to incorrect format
The problem is not knowing if the date is in d/m/Y or m/d/Y which results in sorting the documents by the wrong month.
These documents are coming from different businesses around the world and I have no control over the date format.
Is there a solution to this problem?
Upvotes: 2
Views: 1264
Reputation: 28712
You should use createFromFormat
See http://carbon.nesbot.com/docs/#api-instantiation
Carbon::createFromFormat("d/m/Y",$datestring)
This way you define how the string should be interpreted, if you know what the input string will be.
Carbon extends php's DateTime class so you might also read up on the DateTime createFromFormat docs at https://secure.php.net/manual/en/datetime.createfromformat.php
Upvotes: 1