Re: Re: Saturdays and Sundays
| From: | Jason Davidson | Date: | Thu, 01 Sep 2005 20:30:02 +0000 |
| Subject: | Re: Re: Saturdays and Sundays | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-221720@lists.php.net to get a copy of this message | ||
Here is an another approach.. if you glance at a calendar, youll notice that
the only times there are 5 sats in a month, is when the 1st of 30 day month
falls on a fri or sat.. or in a 31 day month, a thur, fri, or say..
So, you could simply test the weekday the first of the month has, and the
number of days in the month.. and the num of sats in the month is either 4...
or 5. :)
Jason
On 9/1/05, Brian P. O'Donnell <brian@doctorpc.ca> wrote:
>
> Sorry, I made a mistake. See below:
> ""Brian P. O'Donnell"" <brian@doctorpc.ca> wrote in message
> news:5B.F7.15098.8CF27134@pb1.pair.com...
> >
> > ""Shaun"" <shaunthornburgh@hotmail.com> wrote in message
> > news:05.80.15098.C4FC6134@pb1.pair.com...
> > > Hi,
> > >
> > > Is it possible to get the number of saturdays and sundays for a given
> > month
> > > / year?
> > >
> > > Thanks for your help.
> >
> > Here's another way to do it. Each function will return either 4 or 5. If
> you
> > need both Saturdays and Sundays, just call both functions:
> >
> > <?
> >
> > function get_saturdays($month, $year) {
> >
> > $sat = 4;
> >
> > // time stamp of noon on the first day of the month
> > $first_day = mktime(12, 0, 0, $month, 1, $year);
> >
> > switch ($month) {
> > case 1:
> > case 3:
> > case 5:
> > case 7:
> > case 8:
> > case 10:
> > case 12:
> > if (date("w", $first_day) > 3) {
> > $sat++;
> > }
> > break;
> > case 2:
> > if ((date("L", $first_day) == 1) && (date("w", $first_day)
> > > 5)) {
> > $sat++;
> > }
> > break;
> > case 4:
> > case 6:
> > case 9:
> > case 11:
> > if (date("w", $first_day) > 4) {
> > $sat++;
> > }
> > break;
> > }
> >
> > return($sat);
> >
> > }
> >
> > function get_sundays($month, $year) {
> >
> > $sun = 4;
> >
> > // time stamp of noon on the last day of the month
>
> The following line referenced a variable in the other function. DUH!
> It should be:
> $last_day = mktime(12, 0, 0, $month, date("t", mktime(12, 0, 0, $month,
> 1, $year)), $year);
>
> >
> > switch ($month) {
> > case 1:
> > case 3:
> > case 5:
> > case 7:
> > case 8:
> > case 10:
> > case 12:
> > if (date("w", $last_day) < 3) {
> > $sat++;
> > }
> > break;
> > case 2:
> > if ((date("L", $last_day) == 1) && (date("w", $last_day) <
> > 1)) {
> > $sat++;
> > }
> > break;
> > case 4:
> > case 6:
> > case 9:
> > case 11:
> > if (date("w", $last_day) < 2) {
> > $sat++;
> > }
> > break;
> > }
> >
> > return($sun);
> >
> > }
> >
> > ?>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>