Why Zend Date Is So Nice!

I work with the Zend Framework on a daily basis and use the Zend_Date component all the time. I love the wide variety of date and time constants it supports. With two lines of code you can return something as simple as a weekday to something as obscure as a date for an RSS feed, shown in the example below.

$date = new Zend_Date();
$rssfeed = $date->get(Zend_Date::RSS);

However one of the most elegant uses I’ve used it for on more than one occasion is the example below. This example loops through 11 date values, in turn allowing you to perform a single operation on each date, in my example printing the date to the screen. Each time the date object is incremented by one day using the lovely Zend Date add method. I find this syntax simplifies a complicated procedure and in turn provides code that is very easy to read.

// Start day of today
$startDate = new Zend_Date();

// End day of today + 10 days
$endDate = new Zend_Date();
$endDate->add(10, Zend_Date::DAY);

// If start date end date timestamp is greater than start date then end loop
if ($endDate->get() >= $startDate->get()) {

// Loop through dates
while ($startDate->get() <= $endDate->get()) {

// echo start date to screen
echo $startDate->get(‘dd MMM yyyy’) . ‘

// Increment start date by 1 day
$startDate->add(1, Zend_Date::DAY);

This could be extended using different constants, for example instead of passing the startDate the Zend_Date::DAY constant, you might pass the Zend_Date::MONTH constant and loop through the dates month by month.

This entry was posted in Development. Bookmark the permalink.

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>