2

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

3 Answers 3

3

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"}}
4
  • Tried it but it didn't work. Hmmm. Maybe I missed something. Is {{block type="currentdate/datetoday"}} the proper way to call the module? Even if it's not declared anywhere in the Module Block? O_O
    – jehzlau
    Sep 13, 2014 at 15:38
  • I tried to clear my cache and it's just blank. Do I need to create more files? I'm pretty sure I'm missing something I just don't know what it is. haha :D I only create config.xml and put it inside etc folder together with the Block folder of my custom module. Then I added {{block type="currentdate/datetoday"}} in a CMS page. But nothing appear. O_O
    – jehzlau
    Sep 13, 2014 at 15:46
  • It worked for me but perhaps I missed something. I'll try again and report back!
    – philwinkle
    Sep 13, 2014 at 16:04
  • Thanks philwinkle. Maybe there are files that you have that I don't. O__O
    – jehzlau
    Sep 15, 2014 at 19:22
0

Okay there are a few things missing in phil's answer:

  1. You also need a module XML file in app/etc/modules/ with the name YourCompany_YourModule.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <YourCompany_YourModule>
                <active>true</active>
                <codePool>local</codePool>
            </YourCompany_YourModule>
        </modules>
    </config>
    
  2. The second half of the config.xml had an error (block class name):

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

  3. Lastly I found at least that the actual PHP block with the parent::_tohtml() part had a problem (checking the core code shows a check against a beforetoHTML() returning blank so I commented out the call to parent and replaced with the getText() and it all worked.

    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 $this->getText();
        //parent::_toHtml();
      }
    }
    
0

All previous answers were incomplete. Here are the complete steps.

Create the following file at app/code/local/YourCompany/YourModule/etc/config.xml:

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

Create the following file at 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 $this->getText();
    }
}

Create this file at app/etc/modules/YourCompany_YourModule.xml

<?xml version="1.0"?>
<config>
    <modules>
        <YourCompany_YourModule>
            <active>true</active>
            <codePool>local</codePool>
        </YourCompany_YourModule>
    </modules>
</config>

Add a permission for the block YourModule/datetoday in the admin panel.

Perform a cache refresh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.