{"id":1702,"date":"2021-04-07T19:37:01","date_gmt":"2021-04-07T19:37:01","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/solved-datetime-construct-failed-to-parse-time-string-at-position-0-in-magento-2\/"},"modified":"2025-05-22T10:42:39","modified_gmt":"2025-05-22T05:12:39","slug":"solved-datetime-construct-failed-to-parse-time-string-at-position-0-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/solved-datetime-construct-failed-to-parse-time-string-at-position-0-in-magento-2\/","title":{"rendered":"Solved: DateTime::__construct(): Failed to Parse Time String at Position 0 in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Have you ever faced the error stated below while working with the date field on the checkout page of the Magento 2 store?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">DateTime::__construct(): Failed to parse time string at position 0 : Unexpected character<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If yes, then you are at the right place because we have the exact solution for you!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, I\u2019ve 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&nbsp;<em><strong>Failed to parse time string at position 0 in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check out the solution here:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: DateTime::__construct(): Failed to Parse Time String at Position 0 in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code in the file from which the date field is arriving.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$var = '01\/01\/2001';\n$date = str_replace('\/', '-', $var);\nnewDate = date('Y-m-d', strtotime($date));\n$quote->setDate($newDate);\n$quote->save();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1702","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1702"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1702\/revisions"}],"predecessor-version":[{"id":14392,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1702\/revisions\/14392"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}