PHP Finding First and Last day of a month Print

  • 8

Oh how much we love the ever-so-useful strtotime() function, which acts like the workhorse in so many calendar based operations.  With strtotime() you can create a date for an SQL query in a snap.

$lastMonth_start  = date('Y-m-01', strtotime('last month'));
$lastMonth_stop   = date('Y-m-d', strtotime(date('Y-m-01', strtotime('this month')).' -1 minute'));
echo $lastMonth_start.' '.$lastMonth_stop.'<br>';

$thisMonth_start  = date('Y-m-01', strtotime('this month'));
$thisMonth_stop   = date('Y-m-d', strtotime(date('Y-m-01', strtotime('next month')).' -1 minute'));
echo $thisMonth_start.' '.$thisMonth_stop.'<br>';

$nextMonth_start  = date('Y-m-01', strtotime('next month'));
$nextMonth_stop   = date('Y-m-d', strtotime(date('Y-m-01', strtotime('this month +2 months')).' -1 minute'));
echo $nextMonth_start.' '.$nextMonth_stop.'<br>';



Was this answer helpful?

« Back