Jump to content

[SOLVED] date calculations


Ninjakreborn

Recommended Posts

Ok, let me make this a simple question.

Is the following correct.

<?php
$sfees = "SELECT * FROM cy_members";
$qfees = mysql_query($qfees);
$feeok    = 0; // to report number of successfully billed accounts.
$feeerror = 0; // to report the number of failed account billings.
while ($rowfees = mysql_fetch_array($qfees)) {
	$days = 15;
	$cdate = date("m/d/Y"); 
	$cdate strtotime($date);
	$duedate = $rowfees['system_nextbillingdate']; // due date as timestamp
	$latedays = $date - $duedate; 
	if ($latedays == $days) { 

	}
}
?>

Ok basically I need to get the number of day's they are late, so I can do some math with it.  Will the above properly retrieve the number of late days.

Link to comment
Share on other sites

Also can the above properly account for the fact that it may not be late at all.  If it goes to a user who is not late, will it ignore them completely.

If one is 5 days late it needs to ignore then, 10 day's late ignores them.  If they are actually 15 day's late then I can do the appropriate actions, is the above correct for that, thanks.

Link to comment
Share on other sites

Ok, I was about to implement this, and I understand how it'll work but I also ahve to have the "day" able to change out.

Like the day variable be able to change how many day's it can be late.

Like 1 day they get late fee's all the way up to like 15 days they get late fee's (I need the grace period to be changeable.)

Link to comment
Share on other sites

((system_nextbillingdate-".time().")/86400)

That is the part I don't understand.

The next billing date is there upcoming payment.

But I need to calculate 15 days after there billing cycle (the last date they were billed, which is actually system_lastbillingdate).  What I get from this is get the nextbilling date timestamp - the current time dividied by 1 day's worth of seconds???

Link to comment
Share on other sites

Logic, think about it.

$result = mysql_query("SELECT *,((system_nextbillingdate-".time().")/86400) AS days_late FROM cy_members");

That is the query.  Ok

result = (result holds the informaiton (obviously)

mysql_query (runs a query)

All basic stuff BUT

 

now we have SELECT (get)

the unix time stamp of the next billing cycle MINUS the timestamp of the current date DIVIDED BY 1 day's worth of seconds.  Then save that info in the variable days_late from the table cy_members.

That's it, I just don't understand the logic behind.  I mean I do, but I have to account for the fact that I need to change out the days.  He asked me to start it at ONE day then late change it to 15.  I understand what it's doing, but going through the calculations I don't understand how it works.

Link to comment
Share on other sites

Yet one more reason to do your dates in the DATE type in SQL:

DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other.

 

However, since you're using timestamps, try something like this:

<?php
function datediff($date1, $date2) {
  $day = 60 * 60 * 24;
  $diff = $date2 - $date1;

  return ceil($diff / $day);
}
?>

 

Keep in mind that this could very well return negative days (representing that the date is still in the future).

 

Hope that helps.

Link to comment
Share on other sites

Couldn't get that to work, I finally just modified what I had on the other part and everything work and tested out right.

Atleast that's over with one more part to that and everything is fine.

 

<?php
$sfees = "SELECT * FROM cy_members";
$squery = mysql_query($sfees);
$feeok    = 0; // to report number of successfully billed accounts.
$feeerror = 0; // to report the number of failed account billings.
while ($srow = mysql_fetch_array($squery)) {
$curr = date("m/d/Y"); // get current date
$lastbilled = $srow['system_lastbillingdate']; // last billed
$lastbilleddays = mktime(date("h", $lastbilled), date("i", $lastbilled), date("s", $lastbilled), date("m", $lastbilled), date("d", $lastbilled)+1, date("y", $lastbilled));
$lastbilleddays = date("m/d/Y", $lastbilleddays);
if ($lastbilleddays == $curr && $srow['system_currentdueamount'] != 0) {
	$newamount = $srow['system_currentdueamount'] + "10.00";
	$insertnew = "UPDATE cy_members SET system_currentdueamount = '$newamount' WHERE mem_id = " . $srow['mem_id']; 
	echo $insertnew;
	if (mysql_query($insertnew)) {
			$feeok++;
			$feesuccess = $feeok . " accounts had late fees applied successfully.";

		}else {
			$feeerror++;
			$feefailure = "There was a problem with calculating late fees for  " . $feeerror . " members this month.";
		}
}
// unset variables on passthrough to prevent potential issues.
unset($insertnew);
unset($newamount);
unset($curr);
unset($lastbilled);
unset($lastbilleddays);
}
?>

Link to comment
Share on other sites

I know this is a bit off topic but I just have a few tips for doing things like this. if you make a function to select the users And then if you make a function for updating the user you can just do update_user($user_id) by doing this you will find it easier to debug your code because there will be  considerably less code (and no sql mixed within your logic), oh and you don't need those unsets as you are just overwriting the values with each iteration anyway.

 

Sorry if this is a bit off topic.

 

 

Link to comment
Share on other sites

Yes, but it's still helpful.

I saw people doing this, and they called it "modular programming" but I never fully understood it.  When I try to make a function for something that large it ends up taking awhile, but if I did then I could reuse those functions for other projects.

I will keep that in mind, thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.