Magento – How to put correct date on invoice PDF in Magento 2

dateinvoice-pdfmagento2.3.4

Invoice PDFs are generating the incorrect date, I have this on two sites both on 2.3.4 and en_GB locale

e.g.

$order->getCreatedAt()

2020-02-14 11:14:40

becomes

$this->_localeDate->formatDate(
$this->_localeDate->scopeDate(
$order->getStore(),
$order->getCreatedAt(),
true
),
\IntlDateFormatter::MEDIUM,
false
)

13 August 2019

This is coming form:

vendor/magento/module-sales/Model/Order/Pdf/AbstractPdf.php

Removing the scopeDate() function fixes it, but should I do this or is it a configuration error on my part somewhere?

Best Answer

The same issue for me and solve with this code fix Navigation to this file /vendor/magento/framework/Stdlib/DateTime/Timezone.php

and Replaced this function to :

public function scopeDate($scope = null, $date = null, $includeTime = false)
    {
        $timezone = new \DateTimeZone(
            $this->_scopeConfig->getValue($this->getDefaultTimezonePath(), $this->_scopeType, $scope)
        );
        switch (true) {
            case (empty($date)):
                $date = new \DateTime('now', $timezone);
                break;
            case ($date instanceof \DateTime):
            case ($date instanceof \DateTimeImmutable):
                $date = $date->setTimezone($timezone);
                break;
            default:
                $date = new \DateTime(is_numeric($date) ? '@' . $date : $date);
                $date->setTimezone($timezone);
                break;
        }

        if (!$includeTime) {
            $date->setTime(0, 0, 0);
        }

        return $date;
    }

Run the command and check your result

Related Topic