# DavidPR

New Members

9

0 Neutral

• Rank
Newbie
1. ## Calculate number of days between 2 dates

Okay, I think I've got it worked out. On this particular problem anyway. Thanks for everyone's help.
2. ## Calculate number of days between 2 dates

This is what I have on my test page: \$lastdatepaid = strtotime('2019/06/30'); \$todaydate = date('Y/m/d'); \$todaydate = strtotime(\$todaydate); \$todaymonth = date('m', \$todaydate); \$twodayday = date('d', \$todaydate); \$todayyear = date('Y', \$todaydate); \$oneday = date('j', \$todaydate); // These variables simulate a renter's payment day and month in the database. \$payment_day = 29; \$pay_month = 6; if(\$pay_month == 1){\$newpaymentmonth = 2;} if(\$pay_month == 2){\$newpaymentmonth = 3;} if(\$pay_month == 3){\$newpaymentmonth = 4;} if(\$pay_month == 4){\$newpaymentmonth = 5;} if(\$pay_month == 5){\$newpaymentmonth = 6;} if(\$pay_month == 6){\$newpaymentmonth = 7;} if(\$pay_month == 7){\$newpaymentmonth = 8;} if(\$pay_month == 8){\$newpaymentmonth = 9;} if(\$pay_month == 9){\$newpaymentmonth = 10;} if(\$pay_month == 10){\$newpaymentmonth = 11;} if(\$pay_month == 11){\$newpaymentmonth = 12;} if(\$pay_month == 12){\$newpaymentmonth = 1;} \$threezero = array(4, 6, 9, 11); // months with 30 days // Months that have only 28 days or 30 days, setting the payment day to accommodate months with fewer days. if (\$payment_day > 28 && \$pay_month == 2) { \$payment_day = 28; } elseif(\$payment_day == 31 && in_array(\$pay_month, \$threezero)) { \$payment_day = 30; } else { \$payment_day = \$payment_day; } if(\$newpaymentmonth == 1){\$newpaymentyear = \$todayyear + 1;}else{\$newpaymentyear = \$todayyear;} \$newpaymentdate = strtotime(\$newpaymentyear.'-'.\$newpaymentmonth.'-'.\$payment_day); echo date("Y-m-d", \$newpaymentdate) . " The New Payment Date<br><br>"; \$ddate = new DateTime(\$todayyear.'-'.\$newpaymentmonth.'-'.\$payment_day); \$ddate->add(new DateInterval('P5D')); echo \$ddate->format('Y-m-d') . " - New month payment date with 5 day grace period added.<br><br>"; \$ddate = strtotime(\$ddate); \$xmas = new DateTime(\$ddate); \$today = new DateTime(); \$daysToGo = \$xmas->diff(\$today)->days; echo \$daysToGo; if (\$todaydate > \$ddate){echo "<p>Charge Late Fee.</p>";}else{echo "<p>Do Not charge late fee.</p>";} There may be other errors, but this is the only error I get right now: "on line 60: strtotime() expects parameter 1 to be string, object given" Line 60: \$ddate = strtotime(\$ddate); If I comment out that line I get this: "Fatal error: Uncaught TypeError: DateTime::__construct() expects parameter 1 to be string, object given...DateTime->__construct(Object(DateTime)) #1 {main} thrown in /home/...on line 61" Line 61: \$xmas = new DateTime(\$ddate);
3. ## Calculate number of days between 2 dates

Barand...okay, I'll have to digest that for a while and play with it. Thanks
4. ## Calculate number of days between 2 dates

This code that I'm working on will be for a mini-storage company's online rental payment system. The mini-storage office staff will enter into the database a renter's rental information including the "day" each month that renter's payment is due and their monthly rental price. The day a renter's rental payment is due is the day of the month they rented the unit. If it was me I would have done it differently, making all rent due on the first of the month and pro-rating the beginning and ending month. Just easier that way (seems to me), but it is what it is. I have a case where a renter's payment day is the 30th of each month. I made a snippet to make that day the 28th in February and the same for others whose payment day is on the 31st. Anyway, this guy made his June payment on June 30th. Then he made his July's payment on August 1st. It's past the due day date, but the mini-storage company has a 5-day grace period (on the first month - in a series of late payment months). Meaning if he didn't pay his July 30th payment or his August 30th payment, a late fee would be applied one day after his August's payment due date. No grace period of the second month. The trouble is that my current program saw that he did not make a payment in July so it charged him a \$15 late fee even though he was within the 5-day grace period - August 1st. So my thinking was to take the date of his payment day (2019-07-30) and subtract this date from today (or whenever day the renter logs in to pay his rent) and come up with the number of days differing from those two dates. If those days are <= 5, then no late fee is added, If more than 5 days the late fee is added. Same goes if he's 2 months behind. I take the first month's due day date that was missed, add 1 month to it and then see if today or the day he's logged in is past the due day date. If so, another late fee is added.
5. ## Calculate number of days between 2 dates

\$xmas = DateTime::createFromFormat('Y/m/d', \$ddate); The variables I'm using are in my first post - in the first code section.
6. ## Calculate number of days between 2 dates

Barand the script you provided returned this: A system error occurred. We apologize for the inconvenience. Fatal error: Uncaught Error: Call to a member function diff() on boolean in /home/....150 Stack trace: #0 {main} thrown in /home/... Again, that was with me using a date stored in a variable. I'm obviously not correctly doing something with that variable.
7. ## Calculate number of days between 2 dates

Bad values...no doubt. But I have been unsuccessful in finding any example of a date stored in a variable being used in any datediff, timediff, diff script. Always, the examples use dates that are typed in.
8. ## Calculate number of days between 2 dates

I've tried several scripts to get the number of days between two dates, but none of them will work. Obviously I'm doing something wrong. One thing I know that is causing a problem is that I'm using variables instead of an actual typed in date. Which in my opinion is how most people would do it - variables not actual dates. I've tried these: \$todaydate = date('Y/m/d'); \$now = date('Y-m-d'); \$dd2 = strtotime(\$now); \$thisyear = 2019; \$payment_day = 29; \$pay_month = 6; if(\$pay_month == 1){\$newpaymentmonth = 2;} if(\$pay_month == 2){\$newpaymentmonth = 3;} if(\$pay_month == 3){\$newpaymentmonth = 4;} if(\$pay_month == 4){\$newpaymentmonth = 5;} if(\$pay_month == 5){\$newpaymentmonth = 6;} if(\$pay_month == 6){\$newpaymentmonth = 7;} if(\$pay_month == 7){\$newpaymentmonth = 8;} if(\$pay_month == 8){\$newpaymentmonth = 9;} if(\$pay_month == 9){\$newpaymentmonth = 10;} if(\$pay_month == 10){\$newpaymentmonth = 11;} if(\$pay_month == 11){\$newpaymentmonth = 12;} if(\$pay_month == 12){\$newpaymentmonth = 1;} \$threezero = array(4, 6, 9, 11); // months with 30 days // Deal with months that have only 28 days or 30 days, setting the payment day to accommodate months with fewer days. if (\$payment_day > 28 && \$pay_month == 2) { \$payment_day = 28; } elseif (\$payment_day == 31 && in_array(\$pay_month, \$threezero)) { \$payment_day = 30; } else { \$payment_day = \$payment_day; } if (\$newpaymentmonth == 1){\$newpaymentyear = \$thisyear + 1;}else{\$newpaymentyear = \$thisyear;} \$newpaymentdate = date(\$newpaymentyear.'-'.\$newpaymentmonth.'-'.\$payment_day); echo date("Y-m-d", \$newpaymentdate) . "<br><br>"; \$ddate = new DateTime(\$thisyear.'-'.\$newpaymentmonth.'-'.\$payment_day); \$ddate->add(new DateInterval('P5D')); echo \$ddate->format('Y-m-d') . " - New month payment date with 5 day grace period added.<br><br>"; Now, how to calculate the difference in days between today's date and \$ddate? I tried the below, but none worked. function DateDiff(\$strDate1,\$strDate2){ return (strtotime(\$strDate2) - strtotime(\$strDate1))/ ( 60 * 60 * 24 ); // 1 day = 60*60*24 } echo "Date Diff = ".DateDiff(\$now,\$ddate)."<br>"; \$timeDiff = abs(\$now - \$ddate); \$numberofdays = \$timeDiff/86400; echo "<p>\$numberofdays - days between today's date and payment w/grace date.</p>"; \$date1 = \$now; \$date2 = \$ddate; \$diff = date_diff(\$date1,\$date2); echo 'Days Count - '.\$diff->format("%a"); \$date1=\$now; \$date2=\$ddate; function dateDiff(\$date1, \$date2) { \$date1_ts = strtotime(\$date1); \$date2_ts = strtotime(\$date2); \$diff = \$date1_ts - \$date2_ts; return round(\$diff / 86400); } \$dateDiff= dateDiff(\$date1, \$date2); printf("Difference between in two dates : " .\$dateDiff. " Days "); print "</br>"; This one returns 18112 Days. Should be days 7 days from 2019-08-05. None of these work, so I'm doing something wrong. Any ideas? Thanks
9. ## Need help with an arrya index

I think he means that \$_post should be written as \$_POST... \$key=array(\$_post['abc']); to \$key=array(\$_POST['abc']);
×

• #### Activity

• Chat
×
• Create New...