I have this php
code
$today_date = date('d/m/Y H:i:s');
$Expierdate = '09/06/2017 21:45:03';
$remaindate = date_diff($today_date,$Expierdate);
echo $remaindate;
and i need result from difference between two date.
I have this php
code
$today_date = date('d/m/Y H:i:s');
$Expierdate = '09/06/2017 21:45:03';
$remaindate = date_diff($today_date,$Expierdate);
echo $remaindate;
and i need result from difference between two date.
date_diff()
needs a DateTimeInterface as an argument. In other words, you need to create a DateTime object first, using new DateTime()
as shown below.
$today_date = new DateTime();
$Expierdate = new DateTime('09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
The above would output
90 days
Because today is June 8th, and the format 09/06/2017
is September 6th - because you're using American format (MM/DD/YYYY
).
If you ment June 9th (tomorrow), you need to use European format (MM-DD-YYYY
, note the dash instead of slash). You can alternatively use DateTime::createFromFormat()
to create from a set format, so your current format, 09/06/2017, is interpreted as June 9th. The code would then be
$today_date = new DateTime();
$Expierdate = DateTime::createFromFormat('d/m/Y H:i:s', '09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Output (live demo)
1 days
In any case, $remaindate
holds some properties which can be used (see the manual), or you can format it to your liking by supplying the desired formation into the format()
method.