Get current and past month with Zend Date

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);

Advertisements

About admin

Just another php developer trying to give something back to the community.
This entry was posted in PhP, Tools, Zend Framework and tagged , . Bookmark the permalink.

One Response to Get current and past month with Zend Date

  1. Pingback: Display current date and time

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s