I needed a function that determined the last Sunday of the month. Since it's made for the website's "next meeting" announcement, it goes based on the system clock; also, if today is between Sunday and the end of the month, it figures out the last Sunday of *next* month. lastsunday() takes no arguments and returns the date as a string in the form "January 26, 2003". I could probably have streamlined this quite a bit, but at least it's transparent code. =)
<?php
function getlast($mon, $year) {
$daysinmonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$days = $daysinmonth[$mon-1];
if ($mon == 2 && ($year % 4) == 0 && (($year % 100) != 0 ||
($year % 400) == 0)) $days++;
if ($mon == 2 && ($year % 4) == 0 && ($year % 1000) != 0) $days++;
$lastday = getdate(mktime(0,0,0,$mon,$days,$year));
$wday = $lastday['wday'];
return getdate(mktime(0,0,0,$mon,$days-$wday,$year));
}
function lastsunday() {
$today = getdate();
$mon = $today['mon'];
$year = $today['year'];
$mday = $today['mday'];
$lastsun = getlast($mon, $year);
$sunday = $lastsun['mday'];
if ($sunday < $mday) {
$mon++;
if ($mon = 13) {
$mon = 1;
$year++;
}
$lastsun = getlast($mon, $year);
$sunday = $lastsun['mday'];
}
$nextmeeting = getdate(mktime(0,0,0,$mon,$sunday,$year));
$month = $nextmeeting['month'];
$mday = $nextmeeting['mday'];
$year = $nextmeeting['year'];
return "$month $mday, $year";
}
?>