Magento2: Date incorrect on pdf invoice

Created on 3 Feb 2020  路  56Comments  路  Source: magento/magento2

Preconditions (*)

  1. Magento 2.4-develop
  2. 2.

Steps to reproduce (*)

  1. Go to Sales->Orders, select an order and then try to "print invoices"

Expected result (*)

  1. Date to reflect purchase date

Actual result (*)

  1. Dates are incorrect, we have 2006 and 2008 for orders, before the website was developed....
  2. Dates were fine in 2.3.2.
    #26665issueInvoice.pdf
Sales Fixed in 2.4.x Format is valid Reported on 2.3.2

Most helpful comment

@devchris79: Change source code 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;
    }

will solve this problem. I will make PR.

All 56 comments

Thanks for opening this issue!

Hi @devchris79. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

@devchris79 do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

Same issue here but my dates are way higher.
Screenshot 2020-02-03 at 14 01 43

Maybe a good thing to note is the settings below:
Timezone Sever: UTC
Locale Storeview: nl_NL

Hi @engcom-Delta. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

Thanks for opening this issue!

:white_check_mark: Confirmed by @engcom-Delta
Thank you for verifying the issue. Based on the provided information internal tickets MC-31082 were created

Issue Available: @engcom-Delta, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

@engcom-Delta Why did you edit the post to include a precondition of Locale Storeview: nl_NL?
This is not a precondition that we used.....

It looks like the date is inserted in:
/vendor/magento/module-sales/Model/Order/Pdf/AbstractPdf.php

So this may affect other pdf documents like shipping, credit memo etc.

Credit memos are also affected.

I think this may be because of the changes in #23693. @edenduong does that sound likely?

Thanks for opening this issue!

Hi @devchris79 ,

Thank you for your issue report. I try to reproduce it on my local site but I can not reproduce in my site. In my commit, I try to change the logic of scopeDate similar to the date function. Before that, they are different and the scopeDate didn't support full type of interface.

@devchris79 According your steps as its are now, issue is not reproducible. comment adds more details to how reproduce issue and Locale=Dutch (Netherlands) is important thing to reproduce issue

@magento give me 2.3-develop instance

Hi @anvanza. Thank you for your request. I'm working on Magento 2.3-develop instance for you

Hi @edenduong

Why have you closed this issue? Its been seen by myself, @anvanza and @engcom-Delta

Here is our locale options:

locale

@devchris79 : sorry for my mistake. I didn鈥檛 mean that. I reopened it.

@edenduong No problem :-) Maybe the issue is related to the locale option giving the strange results. It seems @engcom-Delta and @anvanza believe this to be pertinent.

@magento give me 2.4-develop instance

Hi @engcom-Delta. Thank you for your request. I'm working on Magento 2.4-develop instance for you

@edenduong No problem :-) Maybe the issue is related to the locale option giving the strange results. It seems @engcom-Delta and @anvanza believe this to be pertinent.

I reproduce the issue in my local. Thank you @devchris79 :)

Hi @engcom-Delta, here is your Magento instance.
Admin access: https://i-26675-2-4-develop.instances.magento-community.engineering/admin_583c
Login: 8d54cec2 Password: 2a124ed93fd9
Instance will be terminated in up to 3 hours.

@devchris79 This case is not reproducible for me
UPD: Issue is reproduced on 2.4-develop also with Locale=English (United Kingdom) and any Timezone

@devchris79: Change source code 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;
    }

will solve this problem. I will make PR.

@edenduong Thanks for the fix, I have applied this and can confirm it works a treat.

Confirmed fix working on 2.3.4 with en_GB system locale.

I just created Pull Request. Thank you.

Hi, 2.3.4 with same issue.
Where to put this fix please.
Is it addition or change existing code?

Hi @ajwalks,

You need to change the scopedate function in timezone.php, its at path:
/vendor/magento/framework/Stdlib/DateTime/Timezone.php

Update with the changes from @edenduong and recompile.

If you havent done so already, look into one of the composer patch management packages, then download the pull request as a diff, and apply via your main composer file. I think applying patches like this is preferred to editing core.

composer require cweagans/composer-patches
Add section to bottom of main composer.json file

    "extra": {
        "magento-force": "override",
        "composer-exit-on-patch-failure": true,
        "patches": {
            "magento/framework": {
                "1.0": "patches/composer/26701.diff"
            }
        }
    }

Download the patch from https://patch-diff.githubusercontent.com/raw/magento/magento2/pull/26701.diff and save into patches/composer folder

Edit the patch file and remove all "lib/internal/Magento/Framework/" from paths. so paths would be "a/Stdlib/DateTime/Timezone.php" and "b/Stdlib/DateTime/Timezone.php" etc.

Now just do a

composer install

When a new release is out which already incorporates the fix, then remove the patch from your composer file.

It looks like the pull request has issues and has not currently passed the checks, I assume that method would only work on the pull requests that are complete?

@devchris79, thank you, ace.
@gwharton, will look into this thanks. A new process for me to get my head around that so like most things magento would take me the rest of the day!

It looks like the pull request has issues and has not currently passed the checks, I assume that method would only work on the pull requests that are complete?

The errors in the Pull request seem like an issue with the testing framework at the moment. They seem to be occurring randomly on other PR's too. If they are re-run, they will probably pass on the second run. I'll trigger a re-run.

Fix worked a treat, thank you to all involved.

Closing as @edenduong fix and pull request has sorted the issue.

After several reruns, all tests on the PR now pass.

@devchris79: please don't close issues when the PR has not been merged yet (if for some reason the PR gets declined and closed, we wouldn't have an open issue anymore to track this bug).

Thanks!

Hi @devchris79. Thank you for your report.
The issue has been fixed in magento/magento2#26701 by @edenduong in 2.4-develop branch
Related commit(s):

The fix will be available with the upcoming 2.4.0 release.

Won't this fix be applied to 2.3?

I don't know what Magento's plan is for backporting to 2.3. Presumably they are still going to be releasing 2.3.5 etc... so I would assume they will be handling the backporting internally, as we are no longer able to generate backports outside of the magento organisation.

In the meantime....

For each issue I care about, that has been merged into 2.4, I create a diff and apply it using the cweagans patching process to my 2.3 installation. At each version release of 2.3, I check each one in turn and if it has been backported into 2.3 by magento, then I remove my local patch. This way, I should be covered in both scenareos.

If you havent done so already, look into one of the composer patch management packages, then download the pull request as a diff, and apply via your main composer file. I think applying patches like this is preferred to editing core.

composer require cweagans/composer-patches
Add section to bottom of main composer.json file

    "extra": {
        "magento-force": "override",
        "composer-exit-on-patch-failure": true,
        "patches": {
            "magento/framework": {
                "1.0": "patches/composer/26701.diff"
            }
        }
    }

Download the patch from https://patch-diff.githubusercontent.com/raw/magento/magento2/pull/26701.diff and save into patches/composer folder

Edit the patch file and remove all "lib/internal/Magento/Framework/" from paths. so paths would be "a/Stdlib/DateTime/Timezone.php" and "b/Stdlib/DateTime/Timezone.php" etc.

Now just do a

composer install

When a new release is out which already incorporates the fix, then remove the patch from your composer file.

Hi, Should the /patches/composer folder created in the root?
I'm getting an error on the composer install

[Seld\JsonLint\ParsingException]
"./composer.json" does not contain valid JSON
Parse error on line 133:
}
--------------------^
Expected one of: '}', ','

Thanks in advanced, Edwin

Looks like there is some whitespace on the end of line 133 that it doesn't like. There shouldnt be any whitespace at the ends of the lines in a json file. Probably just copy and pasted across.

and yes, the patches folder goes in the root of magento

If it is working right, on composer install it should remove framework, reinstall framework and then apply the patch. Then you're good to go. Once the patch is integrated into mainline, you can remove it from your composer.json when you update.

If it is working right, on composer install it should remove framework, reinstall framework and then apply the patch. Then you're good to go. Once the patch is integrated into mainline, you can remove it from your composer.json when you update.

Thanks, I had to add one } extra in the sjon file. The dates in PDFs are correct now.

I am using Magento 2.3.4

I have applied @edenduong' code to the file located in; /vendor/magento/framework/Stdlib/DateTime/Timezone.php (line 196)

#

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;
}
#

This corects dates on invoices.

If you use Magento with composer, there is a diff for you:
https://pastebin.com/YtzX09gL

@dudzio12
How can I apply this patch with composer?

If you use Magento with composer, there is a diff for you:
https://pastebin.com/YtzX09gL

@JustICTNL:

  1. composer require cweagans/composer-patches
  2. Create root subdirectory called patches and create file `datetime_format_2_3_5.patch.
  3. Add pastebin content to this file
  4. Add to your composer.json:
(...)
 "extra": {
        "magento-force": "override",
        "enable-patching": true,
        "composer-exit-on-patch-failure": true,
        "patches": {
            "magento/framework": {
                "Datetime format": "patches/datetime_format_2_3_5.patch"
            }
        }
    }
}

@JustICTNL:

1. `composer require cweagans/composer-patches`

2. Create root subdirectory called `patches` and create file `datetime_format_2_3_5.patch.

3. Add pastebin content to this file

4. Add to your composer.json:
(...)
 "extra": {
        "magento-force": "override",
        "enable-patching": true,
        "composer-exit-on-patch-failure": true,
        "patches": {
            "magento/framework": {
                "Datetime format": "patches/datetime_format_2_3_5.patch"
            }
        }
    }
}

Do we have to remove this again before upgrading to the next Magento release?
What steps are required (I have never worked with patches in Composer).

You probably should delete it as soon as the new release implementing proper fix will be available.

It's good practise to name the patch with issue number just to not get lost in the future:

"magento/framework": {
  "Datetime format - Magento 2.3.5 - issue 26675": "patches/datetime_format_2_3_5_26675.patch"
 }

@MarcusWolschon: with composer-exit-on-patch-failure set to true, composer will complain if the patch is no longer applicable on a certain version of Magento, which is very handy.
But we always double check all our patches after every new Magento version anyways to make sure they are still needed or not (sometimes a fix is done in a completely different way than the patch and composer wouldn't complain in that case, but that's not happening very often)

@dudzio12 and @MarcusWolschon
I did what you said, but it doesn't work for me :'(

Did I do something wrong, or is there another solution? I had the order date 9-11-2022. In magento is the correct order date.

Magento directory/composer.json

"extra": {
        "magento-force": "override",
        "enable-patching": true,
        "composer-exit-on-patch-failure": true,
        "patches": {
            "magento/framework": {
                "Datetime format": "patches/datetime_format_2_3_5.patch"
            }
        },

DIFF file pasted in file:
Magento Directory/patches/datetime_format_2_3_5.patch

I am using Magento 2.3.4

I have applied @edenduong' code to the file located in; /vendor/magento/framework/Stdlib/DateTime/Timezone.php (line 196)

#

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;
}
#

This corects dates on invoices.

This works now for me. i updgraded to Magento 2.3.5-P1 and now it works.
FInd the file with command:
find . -name Timezone.php
Result: ./vendor/magento/framework/Stdlib/DateTime/Timezone.php

I changed above in ./vendor/magento/framework/Stdlib/DateTime/Timezone.php
And now it works :)

@magento-engcom-team It still persisting even on M2.3.5-p2, can we have this ported/merged to p3 maybe?

https://github.com/magento/magento2/blob/2.3.5-p2/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php

This issue still exists in version 2.3.6.

Was this page helpful?
0 / 5 - 0 ratings