In PHP, is there an easy way to get the first and last date of a month?

I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?


$first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));

See date() in PHP documentation.


First day is always YYYY-MM-01, isn't it? Example: date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))

Last day is the previous day of the next month's first day:

$date = new DateTime("2008-09-01");
$date->modify("-1 day");
echo $date->format("Y-m-d");

The first day of the month is always 1. So it will become

YYYY-MM-01

the last day can be calculated as:

<?php
    $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
    echo "There was $num days in August 2003";
?>

OK, first is dead easy.

date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));

Last is a little trickier, but not much.

date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));

If I remember my PHP date stuff correctly...

**edit - Gah! Beaten to it about a million times...

Edit by Pat:

Last day should have been

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1