Computing Magazine

Get Current and Past Month with Zend Date

Posted on the 13 September 2012 by Akahgy

Description:

Return current and previous months in Zend Framework, considering passing into a new year as well.

Solution:

Zend Framework, through Zend_Date() offers a quick solution for this problem.

With the get() function we can return any part of the current date, and with the sub() function we can substract 1 month to get the previous month date.

Example:

$date = new Zend_Date();
$thisMonth = $date->get(Zend_Date::MONTH);
$thisYear = $date->get(Zend_Date::YEAR);

$date->sub(’1′, ‘MM’);
$lastMonth = $date->get(Zend_Date::MONTH);
$lastYear = $date->get(Zend_Date::YEAR);


Back to Featured Articles on Logo Paperblog