dole doug dole doug - 1 year ago 63
PHP Question

how to get the first and last days of a give month

i wish to rewrite a mysql query which use month() and year() functions to display all the post from certain month which goes to my function as a 'Y-m-d' parameter format, but I don't know how can I get the last day of the given month date.

$query_date = '2010-02-04';
list($y, $m, $d) = explode('-', $query_date);
$first_day = $y . '-' . $m . '-01';

Answer Source

You might want to look at the strtotime and date functions.


$query_date = '2010-02-04';

// First day of the month.
echo date('Y-m-01', strtotime($query_date));

// Last day of the month.
echo date('Y-m-t', strtotime($query_date));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download