Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto Calculate Total DAYS for leave, help please! 1

Status
Not open for further replies.

RaffiqEddy

Programmer
Jan 7, 2002
51
MY
Dear PHP & MySQL Expert...

I want to auto calculate DAYS for leave taken based on date key-in by user:

For example:

Start date: 15-04-2004 -- (Friday)
End date: 18-04-2004 -- (Monday)
Days: 3 -- (calculate automatically)

From the date given above, system will automatically:-
1) Count days as 3 -- assuming working day is 15, 16 and 18 only.
2) Sunday will not be count as working day.

Is this possible with PHP + MySQL(3.32) ??

TIA
 
Fairly simple with php. Just turn your dates into php dates. I try to always use the YYYY-MM-DD format because thats native for MySQL and PHP works great with it. This script will convert your DD-MM-YYYY format to that and then do your calculations.

Code:
<?php
//Get the date to the right format YYYY-MM-DD
$sdArry = explode("-",$_REQUEST['sd']);
$startDate = strtotime($sdArry[2]."-".$sdArry[1]."-".$sdArry[0]);
$edArry = explode("-",$_REQUEST['ed']);
$endDate = strtotime($edArry[2]."-".$edArry[1]."-".$edArry[0]);;

print "Starting date: ".date("Y-m-d \i\s D",$startDate)."<br>";
print "Ending date: ".date("Y-m-d \i\s D",$endDate)."<br>";

//Subtracting dates will give you seconds between.
//Convert that to days.
$daysBetween = (($endDate - $startDate)/60/60/24)+1;

//Remove the Sundays
$leaveDays = ceil($daysBetween - ($daysBetween/7));

//Check for a partial week Sunday
$days = array("Sun"=>0,"Mon"=>1,"Tue"=>2,"Wed"=>3,"Thu"=>4,"Fri"=>5,"Sat"=>6);
if($days[date("D",$endDate)] < $days[date("D",$startDate)]) {
    //Take of 1 more sunday
    $leaveDays = $leaveDays - 1;
}

print "Leave Days: ".$leaveDays;
?>

It looks for you to pass in start date as "sd" and end date as "ed". If you save the file as test.php you would test it with "test.php?sd=15-04-2005&ed=18-04-2005".

Enjoy!

-Dustin
Rom 8:28
 
Hi Dustin, Thank u very much, the code given is exactly what I want, u make my day! -- A star for you too..

Dear DRJ478, Thanks for the link, appreciate it.

Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top