How many Mondays in a month?

This is a discussion on How many Mondays in a month? within the PHP General forums, part of the PHP Programming Forums category; Hi, I have posted a similar question in php.db, but I was wondering if someone could help out with ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-29-2003
Phil Dowson
 
Posts: n/a
Default 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
Reply With Quote
  #2 (permalink)  
Old 09-29-2003
Greg Wiley
 
Posts: n/a
Default RE: [PHP] How many Mondays in a month?

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:pj_dowson@hotmail.com]
> Sent: 29 September, 2003 16:22
> To: php-general@lists.php.net
> 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
>
>

Reply With Quote
  #3 (permalink)  
Old 09-29-2003
Phil Dowson
 
Posts: n/a
Default Re: [PHP] How many Mondays in a month?

Greg,

That worked brilliantly... I bow down to your sheer excellence!!! Thankyou
Thankyou Thankyou



"Greg Wiley" <greg@wileysworld.org> wrote in message
news:EFE0033F416BD511B7D8009027D0D39308EF7BCC@gwhd emnts02.server.demon.net...
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:pj_dowson@hotmail.com]
> Sent: 29 September, 2003 16:22
> To: php-general@lists.php.net
> 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
>
>

Reply With Quote
  #4 (permalink)  
Old 09-29-2003
Greg Wiley
 
Posts: n/a
Default RE: [PHP] How many Mondays in a month?

No problem, just glad it's found some wider use.

Cheers, Greg.

> -----Original Message-----
> From: Phil Dowson [mailto:pj_dowson@hotmail.com]
> Sent: 29 September 2003 17:55
> To: php-general@lists.php.net
> Subject: Re: [php] How many Mondays in a month?
>
> Greg,
>
> That worked brilliantly... I bow down to your sheer
> excellence!!! Thankyou Thankyou Thankyou
>
>
>
> "Greg Wiley" <greg@wileysworld.org> wrote in message
> news:EFE0033F416BD511B7D8009027D0D39308EF7BCC@gwhd emnts02.serv
> er.demon.net...
> 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:pj_dowson@hotmail.com]
> > Sent: 29 September, 2003 16:22
> > To: php-general@lists.php.net
> > 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
>
>

Reply With Quote
  #5 (permalink)  
Old 09-29-2003
Burhan Khalid
 
Posts: n/a
Default Re: [PHP] How many Mondays in a month?

Greg Wiley wrote:
> 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.
>

[ snip ]
> function num_days ($day, $month, $year)
> {

[ snip ]

One tip that I would add is that instead of using individual arguments,
you might want to consider using an array as an argument. This prevents
people from inserting the arguments in the wrong order, which would not
yield the correct results.

Function call would be :

$my_date = array();
$my_date['day'] = "Mon";
$my_date['month'] = "Jan";
$my_date['year'] = "2003";

num_days($my_date);

Of course the indexes could be in any order, as long as they have day,
month, year as keys.

Just a thought, good work though :)

--
Burhan Khalid
phplist[at]meidomus[dot]com
http://www.meidomus.com
Reply With Quote
  #6 (permalink)  
Old 09-29-2003
Greg Wiley
 
Posts: n/a
Default RE: [PHP] How many Mondays in a month?

> -----Original Message-----
> From: Burhan Khalid [mailto:phplist@meidomus.com]
>
> Greg Wiley wrote:
> > 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.
> >

> [ snip ]
> > function num_days ($day, $month, $year) {

> [ snip ]
>
> One tip that I would add is that instead of using individual
> arguments, you might want to consider using an array as an
> argument. This prevents people from inserting the arguments
> in the wrong order, which would not yield the correct results.
>

Thanks, that's a good idea; though I would have to change my form
code to match:-(

When I make this change, where would be a good place to submit it?

Cheers, Greg.
Reply With Quote
  #7 (permalink)  
Old 09-30-2003
Cristian Lavaque
 
Posts: n/a
Default Re: [PHP] How many Mondays in a month?

Burhan Khalid wrote:
> Greg Wiley wrote:
>> 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.
>>

> [ snip ]
>> function num_days ($day, $month, $year)
>> {

> [ snip ]
>
> One tip that I would add is that instead of using individual
> arguments, you might want to consider using an array as an

argument.
> This prevents people from inserting the arguments in the wrong

order,
> which would not yield the correct results.
>
> Function call would be :
>
> $my_date = array();
> $my_date['day'] = "Mon";
> $my_date['month'] = "Jan";
> $my_date['year'] = "2003";
>
> num_days($my_date);
>
> Of course the indexes could be in any order, as long as they

have day,
> month, year as keys.
>
> Just a thought, good work though :)
>
> --
> Burhan Khalid
> phplist[at]meidomus[dot]com
> http://www.meidomus.com


You could also put the input values in dropdown menus, at least
day and month. That way you make sure the input to the funtion is
correct everytime.

Cristian
Reply With Quote
  #8 (permalink)  
Old 09-30-2003
Greg Wiley
 
Posts: n/a
Default RE: [PHP] How many Mondays in a month?

> -----Original Message-----
> From: Cristian Lavaque [mailto:webdesign@anguz.net]
>
> You could also put the input values in dropdown menus, at
> least day and month. That way you make sure the input to the
> funtion is correct everytime.
>


The code is just a function it's not a "page" in itself. I use it
in a form that display checkboxes for each Sunday in a month.
Someone else may use it by providing the arguments via a form in
the manner you suggest. The function, however, is just that, a
function.

Cheers, Greg.
--
Greg Wiley
http://wileysworld.org/


------------------------------------------------------------
Mail was checked for spam by the Freeware Edition of No Spam Today!
The Freeware Edition is free for personal and non-commercial use.
You can remove this notice by purchasing a full license! To order
or to find out more please visit: http://www.no-spam-today.com

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:19 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0