Funny you should ask this because I've been meaning to share some code that
I wrote a few months ago that does exactly this for any day of the week.
Please feel free to use this and make changes. I'd appreciate any
changes/bug fixes being sent back to me though.
<?php
/*
* num_days
*
* A function that takes a day, a month and a year and returns the number of
* occurrences of that day in the given month and year.
*
* Arguments:
* day - the day required
* month - which month are we talking about
* year - which year are we talking about
*
* Returns:
* occ - the number of times the day occurs in the month.
* or NULL if there are invalid parameters.
*/
function num_days ($day, $month, $year)
{
$day_array = array("Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday",
"Sun" => "Sunday");
$month_array = array(1 => "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
/*
* Check our arguments are valid.
*/
/*
* $day must be either a full day string or the 3 letter
abbreviation.
*/
if (!(in_array($day, $day_array) ||
array_key_exists($day, $day_array))) {
echo "num_days: invalid argument. \$day must be day name or
three letter abbreviation";
return;
}
/*
* $month must be either a full month name or its 3 letter
abrreviation
*/
if (($mth = array_search(substr($month,0,3), $month_array)) <= 0) {
echo "num_days: invalid argument. \$month must be month name
or three letter abbreviation";
return;
}
/*
* Now fetch the previous $day of $month+1 in $year;
* this will give us the last $day of $month.
*/
/*
* Calculate the timestamp of the 01/$mth+1/$year.
*/
$time = mktime(0,0,0,$mth+1,1,$year);
$str = strtotime("last $day", $time);
/*
* Return nth day of month.
*/
$date = date("j", $str);
/*
* If the difference between $date1 and $date2 is 28 then
* there are 5 occurences of $day in $month/$year, otherwise
* there are just 4.
*/
if ($date <= 28) {
return 4;
} else {
return 5;
}
}
?>
The other function I have does this:
/*
* nth_day
*
* A function that takes a number and a day and returns the date of
* nth occurrence of that day in the given month and year.
*
* Arguments:
* nth - the nth occurence required
* day - the day required
* month - which month are we talking about
* year - which year are we talking about
*
* Returns:
* date - the date on which the nth day occurs in month and year.
* or NULL for errors.
*/
Give me a shout if anyone wants this one.
Cheers, Greg.
> -----Original Message-----
> From: Phil Dowson [mailto:[EMAIL PROTECTED]
> Sent: 29 September, 2003 16:22
> To: [EMAIL PROTECTED]
> Subject: [PHP] How many Mondays in a month?
>
>
> Hi,
>
> I have posted a similar question in php.db, but I was
> wondering if someone could help out with a non-db issue. I am
> trying to display statistics of visitors to my web site, and
> what I would like to do is show the average number of
> visitors that have visited the site in a given month for a
> certain day e.g..:
>
> Stats for www.mysite.com for 09/2003
>
> Monday - 15 average - 65 total
> Tuesday - 16 average - 66 total
> Wednesday - 14 average - 65 total
> Thursday - 13 average - 63 total
> Friday - 15 average - 65 total
> Saturday - 5 average - 25 total
> Sunday - 6 average - 28 total
>
> I have tried a number of ways to do this, but I cannot work
> out a way to show the number for example Mondays that will be
> in a given month, which I need to work out the average. I can
> work out the total easy enough, just not the average.
>
> TIA
>
> Phil Dowson
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php