Comparing dates and show the remainder left til expiry date in PHP
Get the solution ↓↓↓I am trying to create an application in PHP that have a subscription period of 14 days. In my db table, I have start date and expiry date. Iam a bit confused to show up the expiration banner that says "Your application will expire in --- days" .
$start = explode(' ', $billing->started_at); // to get the date only
$end = explode(' ', $billing->expires_at);
$date1=date_create($start[0]);
$date2=date_create($end[0]);
$diff=date_diff($date1,$date2);
Session::put('days_left', $diff->format("%R%a days"));
Please help
Answer
Solution:
In theory, your code should work but you didn't tell us what the actual issue is so here's how I'd approach it.
If you only want to calculate using the date portion of your timestamps, you can just format it before creating yourDateTime
objects.
<?php
$start = new DateTime(date('Y-m-d', strtotime($billing->started_at)));
$end = new DateTime(date('Y-m-d', strtotime($billing->expires_at)));
$difference = $start->diff($end);
echo $difference->format("Your application will expire in %a days.");
Alternatively, you can simply use the timestamp directly. Depends on what you are trying to achieve.
<?php
$start = new DateTime($billing->started_at);
$end = new DateTime($billing->expires_at);
$difference = $start->diff($end);
echo $difference->format("Your application will expire in %a days.");
Working example: http://sandbox.onlinephpfunctions.com/code/417d4691483f53fe083735d257df4fb49b832c58
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.