[sf-lug] how to detect the first Sunday in PHP
mark at kermit.org
mark at kermit.org
Sun Feb 12 21:58:43 PST 2006
Here is a little function with an example use I just
wrote - I think it should do the trick. Hope you guys
can use it:
<?php
function nextMeeting($nextMonth = false) {
$day=date("j");
$month=date("n");
$year=date("Y");
if ($nextMonth) {
$day=1;
if ($month == 12) {
$month=1;
$year++;
} else {
$month++;
}
}
$dayofweek=date("w");
$firstOfMonth=date("w",mktime(0, 0, 0, $month , 1, $year ));
// firgure out what date is the first Sunday of the month
if ( $firstOfMonth > 0 ) {
$firstSunday= 8 - $firstOfMonth;
} else {
$firstSunday= 1;
}
$firstSundayDate=date("D",mktime(0, 0, 0, $month ,
$firstSunday, $year));
// figure out what date the third monday of the month is
if ( $firstOfMonth > 1) {
$offSet = 8 - $firstOfMonth;
} elseif ( $firstOfMonth == 0 ) {
$offSet=1;
} else {
$offSet=0;
}
$thirdMonday= 15 + $offSet;
$thirdMondayDate=date("D",mktime(0, 0, 0, $month ,
$thirdMonday, $year));
// lets see which of these dates now applies
if ($day <= $firstSunday) {
// we didn't miss the first meeting
$nextMeeting=$firstSunday;
$nextMeetingDate=mktime(0, 0, 0, $month ,
$nextMeeting, $year);
} elseif ( ($day > $firstSunday) && ($day <= $thirdMonday) ) {
// we missed the first meeting of the month, but can
still make the second
$nextMeeting=$thirdMonday;
$nextMeetingDate=mktime(0, 0, 0, $month ,
$nextMeeting, $year);
} else {
// we need to wait until next month
$nextMeetingDate=nextMeeting(1);
$nextMeeting=nextMeeting(1);
}
return $nextMeetingDate;
}
$meeting=nextMeeting();
echo "The next meeting is on " . date('l dS \of F Y', $meeting);
?>
On Feb 9, 2006, at 23:37 , jim stockford wrote:
>
> Vince and I are working on updating the sf-lug web
> site. We've agreed to use apache and php, with
> ambitions to integrate with some kind of database....
>
> I'm supposed to figure out how we can dynamically
> test the current date and update the web page to show
> the next meeting, which is either the first Sunday or the
> third Monday of the month.
> For example, if it's the tenth of February, the web
> site should tell people the next meeting is Monday,
> February 20. On Tuesday, February 21 the web site
> should proclaim the next meeting as Sunday March 5.
>
> So far, walking the output of the cal utility and
> making sense of the date function in PHP is difficult.
> I'll keep working, hoping that someone will point to
> some code that does the trick.
> jim
>
>
>
>
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug
More information about the sf-lug
mailing list