skahan
8/15/2013 - 5:20 PM

First day of previous month, last day of previous month using DATETIME class. Requires PHP 5.3 to work ("first day of" is introduced in PHP

First day of previous month, last day of previous month using DATETIME class. Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3).

<?php
$month_ini = new DateTime("first day of last month");
$month_end = new DateTime("last day of last month");

echo $month_ini->format('Y-m-d'); // 2012-02-01
echo "<br/>";
echo $month_end->format('Y-m-d'); // 2012-02-29
echo "<br/>";
?>