string to date
Closed this issue · 3 comments
Aronastu commented
i am using Ethiopia calendar for the date picker, it pass as string for example =25/07/2012 ethiopian format(dd/mm/yyyy). the problem is in Your documentation i was seen any thing convert string to date format.
thanks arona
SamAsEnd commented
That's a good find
SamAsEnd commented
But this requires me to implement date and time format parser like DateTime::createFromFormat
or something like that to the library.
It's much easier if you split the string and pass it to the factory like this
$et = '01/08/2012'; // Ethiopic date string in dd/mm/yyyy format
[$day, $month, $year] = explode('/', $et); // split it by the delimiter
$date = Andegna\DateTimeFactory::of($year, $month, $day);
and get you Andegna\DateTime
object.
SamAsEnd commented
The factory will throw an InvalidDateException
if you give it an invalid date so you can wrap around it with a try-catch like this
try {
$date = Andegna\DateTimeFactory::of(2012, 8, 42);
} catch (\Andegna\Exception\InvalidDateException $ex) {
// invalid date ...
}
or validate the date first like this
use Andegna\Validator\DateValidator;
[$day, $month, $year] = explode('/', '01/08/2012');
$isValidDate = (new DateValidator($day, $month, $year))->isValid(); // return boolean