Spiga



Determining Last Day of a Given Month

It is often useful to determine what the last day of a given month is. Although a lookup table could tell you what the actual day is (although you would have to still calculate February based on leap years), it is often useful to have an actual time stamp for that day.

Fortunately, through the use of mktime() this is not difficult. This function accepts the following variables in order: hour,minute,second,month,day and year. The powerful part is that mktime() automatically determines zero or negative number for you correctly.

Therefore, whereas entering a month of 3 (March) and a day of 1 returns a stamp for March 1' entering a month of 3 (March) and a day of 1 returns a stamp for February.

Code below demonstrate how to take advantage of mktime() in calculating last-day-of-month time stamps.




//set the default timezone to US/Eastern
date_default_timezone_set('US/Eastern');

//Will return a timestamp of the last day in a month for a specified year
function last_day($month, $year) {

//Use mktime to create a timestamp one month into the future, but one day //less.
//Also make the time for almost midnight, so it can be used as an 'end of //month' boundary
return mktime(23, 59, 59, $month + 1, 0, $year);

}

//Determine the last day for february, 2006
$stamp = last_day(2, 2006);

//Output the result, it will be : 28
echo '

The last day for February in 2006 is: ',date('d', $stamp) , '

';

?>

0 comments:

Cant Find?Try This.