Have you ever faced the error stated below while working with the date field on the checkout page of the Magento 2 store?
DateTime::__construct(): Failed to parse time string at position 0 : Unexpected character
If yes, then you are at the right place because we have the exact solution for you!
For instance, I’ve used my custom date field in the checkout page and storing the date in another format instead of its default, it results in the error that states Failed to parse time string at position 0 in Magento 2.
Check out the solution here:
Solution: DateTime::__construct(): Failed to Parse Time String at Position 0 in Magento 2
Use the below code in the file from which the date field is arriving.
$var = '01/01/2001';
$date = str_replace('/', '-', $var);
newDate = date('Y-m-d', strtotime($date));
If you have any doubts regarding this error, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Get Weekly Updates
Never miss Magento tips, tricks, tutorials, and news.
Thank you for subscribing.
Something went wrong.