Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

main.CRITICAL: DateTime::__construct(): Failed to parse time string (25/02/2022) at position 0

Can I get help with this, (We can not now add this item to your cart.)
i get this issue in my magento 2.3.6, after sellect time to delevery, for configurable product and press ADD TO CARD, The problem appears in code:

$date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();

and here you can see the full code form:

public function date($date = null, $locale = null, $useTimezone = true, $includeTime = true)
{
    $locale = $locale ?: $this->_localeResolver->getLocale();
    $timezone = $useTimezone
        ? $this->getConfigTimezone()
        : date_default_timezone_get();

    switch (true) {
        case (empty($date)):
            return new \DateTime('now', new \DateTimeZone($timezone));
        case ($date instanceof \DateTime):
            return $date->setTimezone(new \DateTimeZone($timezone));
        case ($date instanceof \DateTimeImmutable):
            return new \DateTime($date->format('Y-m-d H:i:s'), $date->getTimezone());
        case (!is_numeric($date)):
            $timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE;
            $formatter = new \IntlDateFormatter(
                $locale,
                \IntlDateFormatter::SHORT,
                $timeType,
                new \DateTimeZone($timezone)
            );

            $date = $this->appendTimeIfNeeded($date, $includeTime);
            $date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();
            break;
    }

    return (new \DateTime(null, new \DateTimeZone($timezone)))->setTimestamp($date);
}

not: The problem only occurs on one of the store views (right to left, AR language)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Replace the "/" with "-" before instantiating the DateTime since you are using european format

$date = $formatter->parse($date) ?: (new \DateTime(str_replace('/', '-', $date)))->getTimestamp();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading