Search code examples
phpzend-framework2view-helpers

Zend Framework 2: formatting a date in a view


I'm new to Zend framework and have decided to try to build a simple app using ZF2.

I have a date that is being pulled from the database where my table gateway extends the AbstractTableGateway, this is pulling my data correctly but i'd like to format my date using Zend_Date and not manipulate the string representation myself in my model / view.

Does anybody have any ideas about how i do this?


Solution

  • Here is my custom datatime helper for reference:

    https://github.com/AlloVince/eva-engine/blob/master/vendor/Eva/View/Helper/Datetime.php

    register this helper as a invokable helper service in your module config file

    'view_helpers' => array(
        'invokables' => array(
            'datetime' => 'Eva\View\Helper\Datetime',
        ),  
    ),
    

    Then call it in view by:

    $this->datetime(time(), 0, 'Y-m-d');  //2012-10-09
    $this->datetime()->jsTime(time()); //Sat Oct 6 17:16:05 UTC+0800 2012