function lastDay($emonth, $eday, $eyear) { /* purpose: determine what the last day of the month is and return this day */ $test=0; while($test < 1) { // is this a valid day for this month? if(checkdate($emonth, $eday, $eyear)) { $eday++; } else { // not a valid date $test=1; return($eday-1); } } } // what is today? $day = date("d"); $month = date("m"); $year = date("Y"); print("Today is $month-$day-$year
");
if($day < 31) {
$ceiling = lastDay($month, $day, $year);
} else {
// no need to call function if it is 31st day of the month
$ceiling = 31;
}
$firstdayT = date("D", mktime(0,0,0,$month,1,$year)); // textual
$firstdayN = date("w", mktime(0,0,0,$month,1,$year)); // numeric
// output the first day of the month for demonstration only
print("The first numeric day of the month is 1 and falls on
a $firstdayT ($firstdayN)");
$lastdayT = date("D", mktime(0,0,0,$month,$ceiling,$year)); // textual
$lastdayN = date("w", mktime(0,0,0,$month,$ceiling,$year)); // numeric
// output the last day of the month for demonstration only
print("
The last numeric day of this month is $ceiling and falls
on a $lastdayT ($lastdayN)");
/* loop to determine which week this is, count from 1st
- last day of the month and increment week counter
each time 6 (Sunday) is reached */
$checkme = $lastdayN;
$week=1;
for($i=1; $i < $ceiling; $i++) {
if($checkme == 6) {
$week++;
$checkme=0;
} else {
// increase the day of the week
$checkme++;
}
if($i == $day) {
// it's today so escape the loop
break;
}
}
$day .= 'th';
print("
The $day day is in week #$week of this month"); ?>