cancel
Showing results for 
Search instead for 
Did you mean: 

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

hi
Can I get help with this,
i get this issue 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);
    }



 

1 REPLY 1

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

i solve it by 

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

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