Reputation: 71
I have a set of dates with different formats as below
3 Mar 2020
Mar 3, 2020
A1 -- this is obviously not a date but how can I escape to convert this to date format?
1I
Code below doesn't work with A1
or 1I
.
if (Carbon::createFromFormat("d M Y", $item)) {
return "valid";
//do my things
}
//if it's not date, then just ignore
Any shortest way to convert the correct date to my date format Y-m-d
?
And how can I escape from converting A1
and 1I
? These lines should be ignored when comes to check the date format.
Upvotes: 0
Views: 693
Reputation: 592
$date = 'Mar 3, 2020'; //
$carbon = Carbon::parse($date);
$desiredFormat = 'd/m/Y';
$formattedString = $carbon->format($desiredFormat);
Upvotes: 1
Reputation: 11
Use toggle script to change the date format:
Use strtotime()
and date()
:
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
Upvotes: 1
Reputation: 8098
just use strtotime:
$string = '3 Mar 2020';
if (strtotime($string)) {
return date('Y-m-d', strtotime($string);
}
Upvotes: 2