How to Get Current Store Date and Time in Magento 2

How to Get Current Store Date and Time in Magento 2

Developers often need to work with date and time in Magento 2. Every store has a unique time zone. Therefore, it is essential to know how the time zone works for Magento 2 stores.

For example, if your store delivers the product only after a specific time of the day, you require to get current store date and time in Magento 2 and prepare product delivery schedule according to the time.

When you save a date in Magento 2 database, it gets stored in the default time zone. But while calculating the delivery time for the above scenario, you need to convert it to the default time zone of the system to fulfil the requirement of conditional shipment like “the shipment must be done after 3 am only”.

While creating a store, you have to configure Magento 2 stores and times zones and to serve the purpose of time conversion due to the time zone differences, I’ve come up with the solution to get current store date and time in Magento 2.

Steps to Get Current Store Date and Time in Magento 2

  1. Create Data.php file and use this code.
timezoneInterface is a considerable part of this logic and one can get date and time in various formats too!

That’s all.

If you have a query regarding this post, feel free to ask in the Comment section below.

I would like to solve your query.

Do consider sharing this post with Magento Community via social media.

Thank you.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 14 Reviews)

He is been with Meetanshi for more than two years now as a Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.

Leave a reply:

Your email address will not be published.