0

Hello there, I'm new to the community and kinda new to PHP and I'll leave it at that.

I am currently developing an E-Leave System for my final project course of my university.

I'm having difficulties in designing a form for a leave application which consist of two date picker (using calendar control) whereby it has two text field which are "Date From" and "Date To" and followed by "Leave Taken" text field which suppose to be automatically updated from the "getWorkingDays()" function that i cant get it to work with the "Leave Taken" textfield which suppose to show a value "X" accordingly to DateFrom and DateTo. I would like for the "Leave Taken" textfield to show automatically when the user has picked the date.

I really appreciate any advice and help in advance.

P/S: Sry for my bad english

Here's my coding:

<?php

error_reporting (E_ALL ^ E_NOTICE);

$dateFrom = $POST_['DateFrom'];
$dateTo = $POST_['DateTo'];

//The function returns the no. of business days between two dates and it skips the holidays
function getWorkingDays($startDate,$endDate,$holidays){
    //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
    //We add one to inlude both dates in the interval.
    $days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;

    $no_full_weeks = floor($days / 7);
    $no_remaining_days = fmod($days, 7);

    //It will return 1 if it's Monday,.. ,7 for Sunday
    $the_first_day_of_week = date("N", strtotime($startDate));
    $the_last_day_of_week = date("N", strtotime($endDate));

    //---->The two can be equal in leap years when february has 29 days, the equal sign is added here
    //In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
    if ($the_first_day_of_week <= $the_last_day_of_week) {
        if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
        if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
    }
    else {
        // (edit by Tokes to fix an edge case where the start day was a Sunday
        // and the end day was NOT a Saturday)

        // the day of the week for start is later than the day of the week for end
        if ($the_first_day_of_week == 7) {
            // if the start date is a Sunday, then we definitely subtract 1 day
            $no_remaining_days--;

            if ($the_last_day_of_week == 6) {
                // if the end date is a Saturday, then we subtract another day
                $no_remaining_days--;
            }
        }
        else {
            // the start date was a Saturday (or earlier), and the end date was (Mon..Fri)
            // so we skip an entire weekend and subtract 2 days
            $no_remaining_days -= 2;
        }
    }

    //The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
//---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
   $workingDays = $no_full_weeks * 5;
    if ($no_remaining_days > 0 )
    {
      $workingDays += $no_remaining_days;
    }

    //We subtract the holidays
    foreach($holidays as $holiday){
        $time_stamp=strtotime($holiday);
        //If the holiday doesn't fall in weekend
        if (strtotime($startDate) <= $time_stamp && $time_stamp <= strtotime($endDate) && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
            $workingDays--;
    }

    return $workingDays;
}

//Example:

$holidays=array("");

?><head>
<script language="javascript" type="text/javascript" src="_includes/datetimepicker.js">

</script>

<link rel="stylesheet" type="text/css" href="tcal.css" />
	<script type="text/javascript" src="tcal.js"></script> 
</head>




  
  
 

        

<tr valign="baseline">
          <td nowrap="nowrap" align="right">&nbsp;</td>
          <td nowrap="nowrap" align="right"><div align="left">Date From:</div></td>
          <td><div>
              <input id="dateFrom" type="text" name="DateFrom" class="tcal" value="" autocomplete="off"/>
        </div></td>
</tr>
<tr valign="baseline">
          <td nowrap="nowrap" align="right">&nbsp;</td>
          <td nowrap="nowrap" align="right"><div align="left">Date To:</div></td> 
          <td><div>
              <input id="dateTo" type="text" name="DateTo" class="tcal" value="" autocomplete="off"/>
        </div></td>
</tr>
        <p>&nbsp;</p>
        <p>Leave Taken (Day): 
          
          
          <input name="" type="text" value="<?php echo (int) getWorkingDays($dateFrom,$dateTo,$holidays); ?>">
        </p>
<p>alternatively:</p>
        
        Leave Taken (Day):
        <?php //hard-coded date input
echo (int) getWorkingDays("2011/10/29","2011/11/8",$holidays)
// => will return 7?>
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by jmichae3
0

have you noticed your last line? put that ?> on its own line. that is something you usually want to do anyway. if you want that comment that way use /* => will return 7*/ ?> make sure you have a space before the ?>

also, that statement is missing a ;

Leave Taken (Day):
        <?php //hard-coded date input
echo (int) getWorkingDays("2011/10/29","2011/11/8",$holidays);
// => will return 7
?>

and this on line 107 looks like a problem:
<input name=""
php uses the name field for $_POST and $_GET...

Edited by jmichae3: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.