PHP:: Get previous month and end day of last month

Posted: October 18, 2010 in Php

PHP Get previous month and end day of last month

//Get previous month and year
$lastmonth = date(“Y-m”,mktime(0, 0, 0, date(“m”)-1, date(“d”), date(“Y”))); //eg .output 2010-09

//Get end date of previous month (use ‘t’)
$endDateOfLastMonth = date(“Y-m-t”, strtotime(“last month”)); //eg. output 2010-09-30

Comments
  1. cincodenada says:

    If you’re using PHP 5.3 or newer, you can use “last day of” or “first day of”, e.g.:

    $endDateOfLastMonth = date(‘Y-m-d’,strtotime(“last day of last month”)); // output 2014-11-30, on 2014-12-31

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