Solved: DateTime::__construct(): Failed to Parse Time String at Position 0 in Magento 2
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.
1 2 3 4 5 |
$var = '01/01/2001'; $date = str_replace('/', '-', $var); newDate = date('Y-m-d', strtotime($date)); $quote->setDate($newDate); $quote->save(); |
That’s it.
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.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
buenas, la variable quote de que dependencia proviene?
Hello Ezequiel,
The quote variable comes from Magento\Quote\Model\QuoteRepository
Thank You.