In Carbon PHP we have a method to parse string to date. if the string is a valid date string then i'd be parsed to a valid Carbon object. I'm curious to know if is there any way we could get the possible format string in which php determines the date would be. for example:
Carbon::parse("01 January, 2018");
then we'll get a valid carbon object. My question is, can we anyway get "d F, Y" which is the actual format specifier php guessed automatically.
Method parse
always return an instance of Carbon\Carbon
. So you should either validate your string before:
Validator::make(
['date_value' => $date],
['date_value' => 'date']
)->validate();
Carbon::parse($date);
or use createFromFormat
method instead:
if (Carbon::createFromFormat('d F, Y', $stringVariable) !== false) {
// valid date
}
Carbon
extends the PHP DateTime
class. The Carbon::parse
method eventually passes the input down to DateTime::__construct
which parses it as described in the PHP Manual Supported Date and Time Formats.