Magento: How to get current date in magento email template

Magento: How to get current date in magento email template

July 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: How to get current date in magento email template

Getting Current Date in Magento Email Templates

Magento email templates are a powerful tool for automating customer communications. However, sometimes you need to include dynamic content like the current date or time. In this post, we’ll explore how to get the current date when an email is sent from your Magento store.

Understanding Magento’s Variable Syntax

First, let’s understand why {{var dateAndTime}} might not be working for you. This variable is used in the template design, but it doesn’t correspond directly to the sending time of the email. It’s designed to display a specified date or time from your product or order data.

Getting the Current Date

To get the current date when an email is sent, we’ll need to create a new block and assign it to our template. Here are the steps:

Step 1: Create a New Block

Open your terminal and navigate to app/code/your_vendor/your_module/Block. Create a new file called EmailCurrentDate.php.

In this file, add the following code:

<?php
namespace YourVendor\YourModule\Block;

class EmailCurrentDate extends \Magento\Framework\View\Element\Template
{
    public function getCurrentDate()
    {
        return date('Y-m-d H:i:s');
    }
}

Note: Replace your_vendor and your_module with your actual vendor name and module name.

Step 2: Update Your Template XML

Next, open the XML file for your template (usually located in app/code/your_vendor/your_module/view/frontend/templates/email) and add the following line inside the <referenceContainer> tag:

<block class="YourVendor\YourModule\Block\EmailCurrentDate" name="current_date_block">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">YourVendor_YourModule::email/current_date.phtml</argument>
    </action>
</block>

Step 3: Create the Template File

Create a new directory /app/code/your_vendor/your_module/view/frontend/templates/email/. Inside this folder, create a file called current_date.phtml.

Add the following code to this file:

<?php echo $block->getCurrentDate(); ?>

Step 4: Update Your Email Template

Finally, open your email template (usually in app/code/your_vendor/your_module/view/frontend/templates/email/your_template_name.html) and add the following line where you want the current date to appear:

{{block type="YourVendor\YourModule\Block\EmailCurrentDate" name="current_date_block"}}

Conclusion

With this setup, your Magento email template will now display the current date and time when each email is sent. This can be particularly useful for order confirmation emails that require real-time information.

Remember to clear your cache after making these changes by running bin/magento cache:clean in your terminal.

For more advanced customizations of Magento email templates, I highly recommend checking out our guide on Enhancing Magento Email Templates for more tips and best practices.

Catalog Extensions Themes Email

Our network