Get last month’s date range in PHP

I needed to get last month’s valid date range in php for running reports. A quick google search showed the top results for accomplishing this used for loops that subtracted/added days from now until casting as a date object returned false.

Needless to say, no way am I doing that. So below I have a code snippet that returns the valid start and end date of the previous month. This could be modified in a variety of ways to suit your needs. Needless to say I was disappointed that strtotime did not accept “First day of last month”.

// this returns a date string which I need, but removing the casting as a date
// returns a unix timestamp value more useful for calculations
$first_of_last_month = date('m/d/y',mktime(0,0,0,date('m')-1,1,date('y')));
$end_of_last_month = date('m/d/y',mktime(0,0,0,date('m'),0,date('y')));
 
// if run on 03/01/11
var_dump($first_of_last_month, $end_of_last_month);
// outputs
// string(8) "02/01/11"
// string(8) "02/28/11"

Significant Revisions

  • May 6th, 2024 Converted to jekyll markdown format and copied to personal site
  • Feb 10th, 2011 Originally published on txcowboycoder wordpress site1

Footnotes

  1. Initial md Generated using https://github.com/jsr6720/wordpress-html-scraper-to-md

    Original Wordpress categories: [‘PHP’]

    Original Wordpress tags: “PHP”, “date”, “monthly date range”, “php”

    Original Wordpress comments: 2 Comments