Current location: Hot Scripts Forums » Programming Languages » PHP » leave calculation


leave calculation

Reply
  #1 (permalink)  
Old 07-26-03, 12:35 PM
superman's Avatar
superman superman is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
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 ;
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
carry forward superman PHP 11 08-12-03 03:50 AM
leave calculation vioss PHP 0 08-03-03 11:18 PM
the carry-forward leave calculation? vioss PHP 0 07-31-03 12:41 PM


All times are GMT -5. The time now is 09:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.