Magento 2 Order Time Discrepancy – Custom Code Fetch Issue

magento2

I am working on creating a custom module to show orders report in admin. Right now I have fetched data from 'sales_order' table using sql query. I know I should not use sql queries to fetch data from database rather I should use magento collection object to fetch the data. But right now I am just checking and will update my code in future and will use magento way to fetch data.

So while fetching the data using sql query the time that it shows for created_at time is different than the time that is saved in database.

This is the screenshot of the time saved in database.
enter image description here

While fetching in the frontend the time is different. This is the sql query that I am using
enter image description here

And this is the data that I received.
enter image description here

I have marked the increment_id and we can see that the id is same, but the time that I received in the array is different than the database.

And also the time in the sales order grid in admin is matching with the database time.
enter image description here

So my question is why the time that I have fetched using sql query is different than the one in the database and how I can correct it. Plz could someone guide me in this.

Best Answer

The admin reports show a localized time based on the timezone configured for the store. To create a SQL query that matches, you can use the DATE_SUB and DATE_ADD functions as needed to match the store's timezone offset.

Related Topic