Magento – Insert Current Date as Widget or Variable in Magento CMS

cmscms-blockmagento-1.9

Is there a way to insert the current data in Magento CMS as variable, widget, or any other method aside from including a .phtml file with php echo of date().?

Right now what I did is include a .phtml files with this contents:

<?php
echo "Today is " . date("Y/m/d") . "<br>";
?>

But is there any better or proper way? Like {{datetoday}} or something. Just like when I'm including a URL, I just type the variable {{store url}}

Best Answer

You can do this with a custom block. Create it once and you can reuse it everywhere.

Create a module with the following config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <YourCompany_YourModule>
            <version>0.0.1</version>
        </YourCompany_YourModule>
    </modules>
    <global>
        <blocks>
            <currentdate>
                <class>Mage_Catalog_Block</class>
            </currentdate>
        </blocks>
    </global>
</config>

Install this block: in File app/code/local/YourCompany/YourModule/Block/Datetoday.php:

<?php

class YourCompany_YourModule_Block_Datetoday extends Mage_Core_Block_Text
{
    protected function _toHtml()
    {
        $date = Mage::getModel('core/date')->date('m-d-Y H:i:s');
        $this->setText($date);

        return parent::_toHtml();
    }
}

How to use

Now in your CMS pages or in a static block all you have to do is use the following short tag:

{{block type="currentdate/datetoday"}}