Datediff not working

Joined
Dec 7, 2024
Messages
3
Reaction score
0
date_diff not working.

Code:
$now = date('Y-m-d');

$pay_day = 2;
$pay_month = 11;
$pay_year = 2024;
$lastdatepaid = $pay_year. "-" .$pay_month. "-" .$pay_day;

echo "<p>Today's Date: $now</p>";
echo "<p>Last Date Paid: $lastdatepaid</p>";


$today = date_create($now);
date_add($today,date_interval_create_from_date_string("1 month, 6 days"));
$nextpaymentdate = date_format($today,"Y-m-d");
echo "<p>Next Payment date: $nextpaymentdate</p>";


$diff = date_diff($today,$nextpaymentdate);
$days = $diff->format("%a days");
echo "<p>Difference in $days</p>";
 
Joined
Jul 4, 2023
Messages
527
Reaction score
70
Check this:
PHP:
$pay_day = 2;
$pay_month = 11;
$pay_year = 2024;
$lastDatePaid = new DateTime("$pay_year-$pay_month-$pay_day");

$now = date('Y-m-d');
echo "<p>Today's Date: $now</p>";
echo "<p>Last Date Paid: " . $lastDatePaid->format('Y-m-d') . "</p>";

$today = new DateTime($now);
$nextPaymentDate = clone $today;
$nextPaymentDate->add(new DateInterval('P1M6D'));
echo "<p>Next Payment Date: " . $nextPaymentDate->format('Y-m-d') . "</p>";

$diff = $today->diff($nextPaymentDate);
echo "<p>Difference in: " . $diff->days . " days</p>";
1733689567372.png
 
Joined
Dec 7, 2024
Messages
3
Reaction score
0
Okay, it works but I'm not sure what's going on. I'll have to digest this for a while and do some googling. At first blush I don't understand the "clone" part. I've never seen that before. And what is the "P" in - new DateInterval('P1M6D'))?

A brief insight what I'm trying to do is (mini-storage rental payments) renter's payment day each month is on the 2nd. I take the last payment (2024-11-2) add 1 month plus 6 days (they have a 5-day grace period after their due day of the 2nd to pay without incurring a $20.00 late fee. I say 6 days because the days under 6 days (<6) is the 5-day grace period. I could do <=5 I guess (P1M5D). Anyway, I deduct today's date from the next payment due date of 2024-12-2 ) + the 5 days to get 2024-12-7 -> If today is within the 5 day grace period I can show a "Pay Now" button. If it's day 6 (2024-12-8) or beyond they have to contact the office to pay - no online payment.

Thanks for the help. I'll study the code you provided and do some research to learn what's going on.
 
Joined
Dec 7, 2024
Messages
3
Reaction score
0
Got it. Every time period will start with “P”, and anything that comes after “T” (if present) will be interpreted as time:

And clone is just that, a clone/copy of a previous variable.

I did change
Code:
$nextPaymentDate = clone $today
- to -
Code:
$nextPaymentDate = clone $lastDatePaid;
and that gave me my next payment date which I added 5 days to it.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,073
Messages
2,570,539
Members
47,195
Latest member
RedaMahuri

Latest Threads

Top