RE: [PHP-DB] Number of working hours in a month.
| From: | Manuel | Date: | Sun, 03 Mar 2002 03:38:09 +0000 |
| Subject: | RE: [PHP-DB] Number of working hours in a month. | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-17425@lists.php.net to get a copy of this message | ||
Hi there,
Thanks, the article was posted by me at that site.
I have a few different email addresses to a point I forgot which and which..
I have solved the problem. Here are the codes.
<?
//
// 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
//
// Initial formula doesn't work well, so I "reversed-engineered" to get the formula.
// 0 - Sunday,...,6 - Saturday
for ($year = 2001; $year <= 2037; $year++)
{
for ($month = 1; $month <= 12; $month++)
{
$num_of_days = date("t", mktime(0,0,0,$month,1,$year));
echo "<HR> Month=$month Year=$year <BR>";
echo "Number of days = $num_of_days <BR>";
$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 = $firstday,$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;
} elseif ($firstday < $day_of_week) {
// correct week, now move forward to specified day
$d = ($day_of_week - $firstday + 1);
} else {
// my "reversed-engineered" formula
if ($lastday==28) // max of 4 occurences each in the month of February with 28 days
$d = ($firstday + 4);
elseif ($firstday==4)
$d = ($firstday - 2);
elseif ($firstday==5 )
$d = ($firstday - 3);
elseif ($firstday==6)
$d = ($firstday - 4);
else
$d = ($firstday - 1);
if ($lastday==29) // only 1 set of 5 occurences each in the month of February with 29 days
$d -= 1;
}
$d += 28; // jump to the 5th week and see if the day exists
if ($d > $lastday) {
$weeks = 4;
} else {
$weeks = 5;
}
if ($day_of_week==0) echo "Sun ";
elseif ($day_of_week==1) echo "Mon ";
elseif ($day_of_week==2) echo "Tue ";
elseif ($day_of_week==3) echo "Wed ";
elseif ($day_of_week==4) echo "Thu ";
elseif ($day_of_week==5) echo "Fri ";
else echo "Sat ";
echo "occurences = $weeks <BR> ";
} // for $day_of_week loop
} // for $mth loop
} // for $year loop
?>
At 04:16 PM 02-03-2002 -0800, Shrock, Court wrote:
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: php-db@lists.php.net 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