Wednesday, October 12, 2011

PHP - Get the last day of a month


<?php
function GetLastDayofMonth($year, $month) {
for ($day=31; $day>=28; $day--) {
if (checkdate($month, $day, $year)) {
return $day;
}
}
}
?>


Then to call the last day of the month

$lastdayofmonth = GetLastDayofMonth(2011, 02);
echo $lastdayofmonth;


This would print the last day of the month of February 2011

The function is in the notes for the checkdate php function at
http://php.net/manual/en/function.checkdate.php

No comments: