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!

Diff between dates..

Status
Not open for further replies.

Copierbw

Technical User
Sep 25, 2002
112
IE
I have two fields(text boxes) where users can select holiday dates with a calendar (from - to)ex.(2002/02/05 - 2002/03/10). I want to calculate the amount of days the person is taking without him/her doing any input into a 3rd text box. How do I do that taking weekends into account because weekends doesn't count as holidays taken? Any ideas or direction?
 
You are kind of contradicting yourself in your post, but from what I understand, you want to calculate the difference between the two dates, counting all days. One way to do it is to convert the dates into UNIX timestamps and then subtract the first date from the second and then divide the result by 86400 (the number of seconds in one day). Like this:
Code:
if (strtotime($_POST['date1']) < strtotime($_POST['date2']))
    $diff = (strtotime($_POST['date1']) - strtotime($_POST['date2'])) / 86400;
else
    echo &quot;The first date is greater than, or equal to the second date.&quot;;
$diff should then contain the number of days between the two dates. //Daniel
 
What I actually ment with &quot;&quot;How do I do that taking weekends into account because weekends doesn't count as holidays taken?&quot;&quot; is that weekends doesn't count as holidays taken and must be subtracted from the total amount of days. Date2 - Date1 - weekend days = total amount of days taken.
Now I guess that's not as simple to do or am I wrong?:)
 
Well, if weekends are the only days that has to be substracted, it's not really that hard :)
Something like this should work:
Code:
$time1 = strtotime($_POST['date1']);
$time2 = strtotime($_POST['date2']);
$day1 = date(&quot;w&quot;, $time1);
$day2 = date(&quot;w&quot;, $time2);
$days = 0;
$weeks = date(&quot;W&quot;, $time2) - date(&quot;W&quot;, $time1);
if ($day1 < $day2 && $day1 != 0 && $day1 != 6)
    $days = $day2 - $day1;
elseif ($day1 > $day2)
{
    for ($i = $day1, $j = 0; $i > $day2; $i--, $j++)
        ;
    $weeks--;
    $days = $j + 5;
}
elseif ($day1 < $day2)
{
    if (($day1 == 0 || $day1 == 6) && ($day2 != 0 && $day2 != 6))
        $days = $day2;
    elseif (($day1 != 0 && $day1 != 6) && ($day2 == 0 || $day2 == 6))
        $days = 5 - $day1;
}
$days += $weeks * 5;
I'm pretty sure that this won't work, so please post your errors when you get them. :) //Daniel
 
Here's a function I wrote once to do something similar. The function has an array $holidays in it -- you store paid leave days (days you get paid for, but don't have to work) as &quot;mm-dd&quot; and it will also not count those days against vacation time.

[tt]
Code:
function count_workdays($begin, $end)
{
	$holidays = array ('12-25','1-1');
	
	$retval = 0;
	
	$unix_begin = strtotime($begin);
	$unix_end = strtotime($end);
	
	while ($unix_begin <= $unix_end)
	{
		$date_array = getdate($unix_begin);
		
		if ($date_array['wday'] != 0 && $date_array['wday'] != 6)
		{
			$count_day = 1;
			$counter = 0;
			while ($counter < sizeof($holidays) && $count_day == 1)
			{
				$holiday = split (&quot;-&quot;, $holidays[$counter]);
				if (($holiday[0] == $date_array['mon']) && ($holiday[1] == $date_array['mday']))
				{
					$count_day = 0;
				}
				
				$counter++;
			}
			
			$retval += $count_day;
		}
		
		$unix_begin += 86400;
	}
	
	return $retval;
}
[/tt] ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top