I just ran across this description[1] that points to this link[2].  HAven't
tried it personally, and the site requires a login, so you might not find it
useful, but just in case.

[1]Count how many weeks in the month have a specified day, such as Mon, Tue,
etc. Var avail - number of days - first dayname of the month, occurences of
Sun, occurences of Mon, etc. Allows you to calculate number of working hours
exclude Holidays.
[2] http://www.weberdev.com/get_example.php3?count=3267

-----Original Message-----
From: Hoo Kok Mun
To: [EMAIL PROTECTED]
Sent: 3/2/02 1:31 AM
Subject: [PHP-DB] Number of working hours in a month.

Dear all,

How do I dynamically calculate how many working hours in a particular
month?
Has anyone do it before?

Here is the simple formula.
Add - Number of days in a month
Deduct - Sundays
Number of Mon-Fri in a month * 8 hours
Number of Sat in a month * 4 hours
My Figure = total working hours of a month.
I do not need to include any holidays.

This data is for my monthly timesheets.
Currently I am hard coding it while I find a solution...

I have almost managed to get the info that I wanted.
Below is the script and it works almost for all the months except for
some...
I would say about 92%, 1 in 12 incorrect...

Can anyone help to debug and find a formula to get this working?

<?
$month=11;
$year=2002;

$num_of_days = date("t", mktime(0,0,0,$month,1,$year));
echo "Month=$month Year=$year <BR>";
echo "Number of days = $num_of_days <BR>";

// count how many weeks in the month have a specified day, such as
Monday.
// we know there will be 4 or 5, so no need to check for $weeks<4 or
$weeks>5

$firstdayname = date("D", mktime(0, 0, 0, $month, 1, $year));
$firstday = date("w", mktime(0, 0, 0, $month, 1, $year));
$lastday = date("t", mktime(0, 0, 0, $month, 1, $year));
echo "First day of the month = $firstdayname <BR> ";

for ($day_of_week = 0; $day_of_week <= 6; $day_of_week++)
{
     if ($firstday > $day_of_week) {
         // means we need to jump to the second week to find the first 
$day_of_week
         $d = (7 - ($firstday - $day_of_week)) + 1;
         echo "d=$d ";
     } elseif ($firstday < $day_of_week) {
         // correct week, now move forward to specified day
         $d = ($day_of_week - $firstday + 1);
         echo "d=$d ";
     } else {    // $firstday = $day_of_week
         // correct day in first week
         $d = ($firstday - 1);
         echo "d=$d ";
     }

     $d += 28;    // jump to the 5th week and see if the day exists
     echo "(Final D=$d > $lastday) ";
     if ($d > $lastday) {
         $weeks = 4;
     } else {
         $weeks = 5;
     }
     echo "$day_of_week occurences = $weeks <BR> ";
  }
?>


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to