Reusing Magento’s calendar control

Reusing Magento’s calendar control

One of the cool things in Magento is that there is so many controls (widgets, blocks), etc. that you can reuse on the frontend or on the backend. One such control is the calendar control. Calendar control is mostly just used on the backend, aka “adminhtml” area. You can easily figure that one out if you do a quick search for a “Calendar.setup” string on “app/design” folder, in which case you would get several search results but all from “app/design/adminhtml/” folder.

So the question is how to get the calendar (from image above) to show on the frontend area? Easily, all you need is to include some CSS, few JavaScript’s, image and you are done. To make things more flexible, I wrote a little block class that does this for you.

class Inchoo_Module_Block_Calendar extends Mage_Core_Block_Template
    protected function _construct()
    protected function _prepareLayout()
        return parent::_prepareLayout();        
    private function _injectCalendarControlJsCSSInHTMLPageHead()
                array('template' => 'page/js/calendar.phtml')
                ->addItem('js_css', 'calendar/calendar-win2k-1.css')
        return $this;

Please note that I placed this block class under the Inchoo/Module namespace, where you can easily make fully new YourCompany/Calendar extension and just put this in it, if you wish your entire extension just for calendar control.

Hopefully, _construct() method is clear to everyone in this case?! If not, then please skip this article entirely and jumo to the Magento Wiki 🙂

The core of the functionality here lies in the _injectCalendarControlJsCSSInHTMLPageHead() method which is called by _prepareLayout() method. If you study it closely you will see that _injectCalendarControlJsCSSInHTMLPageHead() adds the necessary CSS and JS to the head portion of the HTML page that gets rendered.

With this approach, all we need to do later in our XML layout files is just call our Calendar block like:

<block type="inchoo/calendar" name="inchoo.calendar" />

And finally the content of your app/design/frontedn/SOMEPACKAGE/SOMETHEME/template/inchoo/calendar.phtml file should look like:

    <img style="" title="Select Date" id="date_select_trig" class="v-middle" alt="" src="<?php echo $this->getSkinUrl("images/calendar.gif");?> "/>
    <input type="text" style="width: 120px;" class="input-text" value="" id="selected_date" name="selected_date"/>
    <script type="text/javascript">
            inputField: "selected_date",
            ifFormat: "%m/%e/%Y %H:%M:%S",
            showsTime: true,
            button: "date_select_trig",
            align: "Bl",
            singleClick : true

If all of the three files are in place then you should be able to see something like shown on image below.

Additionally, please keep in mind that this is really basic approach. You could do a bit more abstraction to it so that XML layout block accepts few more parameters that would be passed to a Calendar.php block class, like show a bit different calendar, do a bit different format pickup, etc. so that you can truly reuse the same control in various ways.

P.S. If Magento is having trouble loading calendar/calendar.js, calendar/calendar-setup.js, calendar/calendar-win2k-1.css then try doing a seach on entire installation and moving them to your theme then modify the addItem, addJs in your Calendar.php block class.

Hope this was helpful.

You made it all the way down here so you must have enjoyed this post! You may also like:

Featured products in Magento 2 with Catalog Products List widget Damir Korpar
Damir Korpar, | 12

Featured products in Magento 2 with Catalog Products List widget

Programmatically add a custom block in Magento Admin Kristijan Glibo
Kristijan Glibo, | 4

Programmatically add a custom block in Magento Admin

Modify “Interface locale” dropdown in administration Tomas Novoselic
, | 18

Modify “Interface locale” dropdown in administration


  1. Hello,

    Magento calendar auto select active date on next/ pre click of month and year button.
    How I can disable this feature?
    This is creating issue when I disable previous months on calendar. User are able to set disabled dates.

    Upma Gupta

  2. Can we add two buttons in Calendar.setup({ .
    I want to display a calendar on textbox as well as on Calendar icon click. How to do this?

  3. Hi,

    Above code disables all the date. So, I changed it a bit and its working for me now.

    //getId() . ‘”,
    ifFormat : “‘ . $displayFormat . ‘”,
    showsTime : “‘ . ($this->getTime() ? ‘true’ : ‘false’) . ‘”,
    button : “‘ . $this->getId() . ‘_trig”,
    align : “Bl”,
    singleClick : true,
    disableFunc: function(date) {
    var now= new Date();
    if(date.getFullYear() < now.getFullYear()) { return true; }
    if(date.getFullYear() == now.getFullYear()) { if(date.getMonth() < now.getMonth()) { return true; } }
    if(date.getMonth() == now.getMonth()) { if(date.getDate() < now.getDate()) { return true; } }

  4. Use the folowing to disable the previous dates:

    <script type="text/javascript">
            inputField : '_dob',
            ifFormat : '%m/%e/%y',
            button : '_dob_trig',
            align : 'Bl',
            singleClick : true,
            disableFunc: function(date) {
              var now= new Date();
                return true;
                    return true;
                    return true;
  5. To reuse the template on several places, just modify the input field like this:

    <input type="text" style="width: 120px;" class="input-text" value="" id="selected_date" name="<?php echo $this->getInputName() ?>"/>

    and in XML, call the block like this:

    <block type="inchoo/calendar" name="frontend_calendar">
                        <action method="setInputName"><input_name>dob</input_name></action>
  6. Hi Branko,
    Thanks for this great post.
    I want to use two calendars for start date and end date.
    I have got that working with two separate script tags with different identifiers but is there a better way to perform this?
    Any help is much appreciated

  7. Is there any way to show up the Embedded Calender, rather than on click event.

    I am using the same logic up there, magento default calendar.


  8. I fixed the issue by adding the calendar block as:

    	<block type="inchoo/calendar" name="inchoo.calendar" />

    Also i have one question: I don’t really find the usage of template=calendar.phtml, How to call it from required .phtml?


  9. Hi Branko

    I liked the approach. But when i tried to use the block class i got the following error:
    Fatal error: Call to a member function append() on a non-object in ..
    Note that i am using this code in checkout_onepage_shippingmethod handle.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.