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

Time:02-25

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)

CodePudding user response:

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

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