-3

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.

Obada Diab
  • 77
  • 6
  • The difference wouldn't be a date; it would be years, days, hours, minutes, seconds. I would rename `$remaindate` to `$remaintime` – Shammel Lee Jun 07 '17 at 22:40
  • ... and see mike's answer at that page – mickmackusa Jun 07 '17 at 22:44
  • guys how can i edit this question to got positive? i banned from ask questions! – Obada Diab Jun 14 '17 at 00:59
  • @ObadaDiab You should read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [SO META: Banned from asking questions?](https://meta.stackexchange.com/questions/86997/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th) – Qirel Jun 14 '17 at 11:23

1 Answers1

1

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');

Live demo

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.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Probably better to use `DateTime::createFromFormat` for the expire date as I assume that date is supposed to be `09-Jun` (`day/month`) instead of what DateTime expects which would return `06-Sep` (`month/day`). – Jonathan Kuhn Jun 07 '17 at 22:46
  • That is true, the format given in the original answer is in American format (MM/DD/YYYY). I'll edit in a note about it. – Qirel Jun 07 '17 at 22:46