Not a Number (NAN)
Posted: Thu Jul 07, 2011 12:54 pm
Hello all...I'm relatively new to PHP but not to programming. I'm running into a weird issue. I'm writing a type of accounting web site for my work. It keeps track of payments made by each employee for a staple fund. Payment is due every two weeks and I have a MySQL db that keeps the date a person paid, amount paid, how much their next payment should be and the date that payment is due. All relatively simple.
I have a screen where I ask for what pay period a user wants to view and then the PHP code determines each persons balance up to that pay period. I got it to work, but as I try to run it multiple times, the $num_payperiods ends up going to NAN. It isn't because of the dates I'm giving because if I shut down the browser (Chrome) and stop Apache and MySql sessions and start them back up and retype in the same date it will work. But then as I go back to enter a different date I eventually get NAN. It will go to NAN after entering 2 to 3 dates. Here is the code:
I've put extraneous echos in to help me debug. If I put in 7/1/2011 for a date it will work fine. If I then enter say 6/17/2011 it may or may not work. If it doesn't I shut everything down, start it back up and then enter 6/17/2011 I get the correct values.
Any idea of what is going on?
Thanks
cl2606
I have a screen where I ask for what pay period a user wants to view and then the PHP code determines each persons balance up to that pay period. I got it to work, but as I try to run it multiple times, the $num_payperiods ends up going to NAN. It isn't because of the dates I'm giving because if I shut down the browser (Chrome) and stop Apache and MySql sessions and start them back up and retype in the same date it will work. But then as I go back to enter a different date I eventually get NAN. It will go to NAN after entering 2 to 3 dates. Here is the code:
Code: Select all
function calculate_balance($due_date) {
// calculate the balance due for each person based on the due date given
$payment_history = get_last_payment_all();
// hold the balance for each person
$balance = array();
foreach ($payment_history as $history) {
$last_due_date = new DateTime($history['nextDateDue']);
// determine the span (number of days) between due date given
// and last due date on record.
$span = $last_due_date->diff($due_date);
if ($last_due_date == $due_date) {
// if the two dates are equal the balance is the amount in the database.
$balance[] = $history['amount_due'];
} else if ($span->format('%R') == '-') {
// if the last due date on record is greater than the due date given
// the person is at least paid up to this date so balance = 0.
$balance[] = 0.0;
} else {
echo "<b>".$history['lastName']."</b><br />";
echo "last_due_date".$last_due_date->format('m/d/Y')."<br />";
echo "due_date".$due_date->format('m/d/Y')."<br />";
$num_days = $span->format('%d');
echo "num_days = ".$num_days."<br />";
$num_months = $span->format('%m');
echo "num_months = ".$num_months."<br />";
$days = ($num_months * 30) + $num_days;
echo "days = ".$days."<br />";
//$len = strlen($days);
$number_days = intval($days);
echo "num days = ".$number_days."<br />";
// determine the number of pay periods
// (1 pay period = 2 weeks = 14 days)
$twoWeeks = 14;
$num_payperiod = $number_days / $twoWeeks;
echo "Num payperiod = ".$num_payperiod."<br />" ;
// temp balance from last date paid to the due date given
//$rate = floatval($history['biWeeklyRate']);
$temp_bal = $history['biWeeklyRate'] * $num_payperiod;
echo "temp_bal = ".$temp_bal."<br />";
// total balance for this person is their previous balance
// plus the temp_bal
$balance[] = $history['amount_due'] + $temp_bal;
} // if ($history['nextDateDue' > $due_date)
} // foreach ($payment...)
return $balance;
} // function calculate_balance($due_date)Any idea of what is going on?
Thanks
cl2606