superman Smarty Rookie
Joined: 08 Jul 2003 Posts: 11
|
Posted: Sat Jul 26, 2003 4:37 pm Post subject: leave calculation |
|
|
I have a code that calculate the duration between 2 dates, it can exclude saturday, sunday and public holiday.
OK, the problem now is I don't know how to calculate the balance.
1 new staff only can apply leave for maximum 18 days a year.
After 2 years he joined the company, he has 19 days. First 2 years he only has 18 days.
For example, he started at 01/01/2000, when come to 01/01/2003, he has 19 days a year.
When come to 01/01/2004, he has 20 days, when come to 01/01/2005, he has 21 days and so on.
Before he can apply the leave, the php will show the balance that he can apply the leave.
Lets say he has 16 days, he wants to apply from 01/07/2003 to 07/07/2003, 5 days(exclude saturday & sunday).
Then the script will minus the 5 days, (16-5=11) 11 days left, if the duration is large than 16 days, the script will prompt the error message.
Can you help me? Is it possible to do that? Thank you, thanx a lot...
----------------------------------------------------------------------------------------
This is the code that calculate the duration between 2 dates (exclude saturday & sunday)
----------------------------------------------------------------------------------------
<?php
function NumHolidays($Start, $End)
{
$dbConn = MySQL_Connect("localhost", "root", "") or
Die("Error connecting to Server");
MySQL_Select_DB("mw") or
Die("Error Connecting to Database");
$sSQL = "SELECT * FROM Holidays";
$recHolidays = MySQL_Query($sSQL) or
Die("Invalid Query");
$NumHolidays = 0;
while ($Holiday = MySQL_Fetch_Array($recHolidays))
{
$dtDate = StrToTime($Holiday["Date"]);
$dtStart = StrToTime($Start);
$dtEnd = StrToTime($End);
if (Date("m", $dtStart) < Date("m", $dtDate))
{
if (Date("m", $dtDate) <= Date("m", $dtEnd))
{
if (Date("d", $dtDate) <= Date("d", $dtEnd))
{
$NumHolidays++;
}
}
}
else if (Date("m", $dtStart) == Date("m", $dtDate))
{
if (Date("d", $dtStart) <= Date("d", $dtDate))
{
if (Date("m", $dtDate) <= Date("m", $dtEnd))
{
if (Date("d", $dtDate) <= Date("d", $dtEnd))
{
$NumHolidays++;
}
}
}
}
}
return $NumHolidays;
}
function count_workdays($date1,$date2){
$firstdate = strtotime($date1);
$lastdate = strtotime($date2);
$firstday = date(w,$firstdate);
$lastday = date(w,$lastdate);
$totaldays = intval(($lastdate-$firstdate)/86400)+1;
//check for one week only
if ($totaldays<=7 && $firstday<=$lastday){
$workdays = $lastday-$firstday+1;
//check for weekend
if ($firstday==0){
$workdays = $workdays-1;
}
if ($lastday==6){
$workdays = $workdays-1;
}
}else { //more than one week
//workdays of first week
if ($firstday==0){
//so we don't count weekend
$firstweek = 5;
}else {
$firstweek = 6-$firstday;
}
$totalfw = 7-$firstday;
//workdays of last week
if ($lastday==6){
//so we don't count sat, sun=0 so it won't be counted anyway
$lastweek = 5;
}else {
$lastweek = $lastday;
}
$totallw = $lastday+1;
//check for any mid-weeks
if (($totalfw+$totallw)>=$totaldays){
$midweeks = 0;
} else { //count midweeks
$midweeks = (($totaldays-$totalfw-$totallw)/7)*5;
}
//total num of workdays
$workdays = $firstweek+$midweeks+$lastweek;
}
return ($workdays);
} //end funtion count_workdays()
$date1 = "$FromDate";
$date2 = "$ToDate";
$Duration = count_workdays($date1, $date2) - NumHolidays($date1, $date2);
echo $Duration ;
?> |
|