Ninjakreborn Posted May 3, 2007 Share Posted May 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/ Share on other sites More sharing options...
Ninjakreborn Posted May 3, 2007 Author Share Posted May 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244292 Share on other sites More sharing options...
jitesh Posted May 3, 2007 Share Posted May 3, 2007 PHP ---- echo (strtotime('1998-10-07') - strtotime('1997-10-07'))/86400; Mysql ----- SELECT (TO_DAYS('1998-10-07') - TO_DAYS('1997-10-07')) AS DAYS; Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244293 Share on other sites More sharing options...
Daniel0 Posted May 3, 2007 Share Posted May 3, 2007 Change to fit your application: <?php $result = mysql_query("SELECT *,((system_nextbillingdate-".time().")/86400) AS days_late FROM cy_members"); ?> If days_late is negative or zero, then they aren't late. Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244298 Share on other sites More sharing options...
Ninjakreborn Posted May 3, 2007 Author Share Posted May 3, 2007 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.) Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244305 Share on other sites More sharing options...
Ninjakreborn Posted May 3, 2007 Author Share Posted May 3, 2007 ((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??? Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244327 Share on other sites More sharing options...
Ninjakreborn Posted May 3, 2007 Author Share Posted May 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244333 Share on other sites More sharing options...
Ninjakreborn Posted May 3, 2007 Author Share Posted May 3, 2007 Anybody, this is driving me crazy. Anyone have a functional function (take's date 1, date 2) and returns the amount of day's they are apart. Need to figure out something here. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244403 Share on other sites More sharing options...
obsidian Posted May 3, 2007 Share Posted May 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244424 Share on other sites More sharing options...
Ninjakreborn Posted May 3, 2007 Author Share Posted May 3, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244471 Share on other sites More sharing options...
Nameless12 Posted May 3, 2007 Share Posted May 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244521 Share on other sites More sharing options...
Ninjakreborn Posted May 3, 2007 Author Share Posted May 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49805-solved-date-calculations/#findComment-244524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.