19

They have the same format:

$date_expire = '2014-08-06 00:00:00';
$date1 = date("Y-m-d G:i:s");
$date2 = date_create($date_expire);

$diff = date_diff($date1, $date2); //this line makes error.

But I am getting this error:

date_diff() expects parameter 1 to be DateTimeInterface, string given

Dale K
  • 25,246
  • 15
  • 42
  • 71
ShelðÔn Alag
  • 367
  • 2
  • 3
  • 12

2 Answers2

58

Because you are passing string whereas date_diff expects datetime object,

$date_expire = '2014-08-06 00:00:00';    
$date = new DateTime($date_expire);
$now = new DateTime();

echo $date->diff($now)->format("%d days, %h hours and %i minuts");

DEMO.

Eem Jee
  • 1,239
  • 5
  • 30
  • 64
Rikesh
  • 26,156
  • 14
  • 79
  • 87
1
<?php
$todays_date = date("m/d/Y H:i:s"); 

$exp = date("m/d/Y H:i:s", strtotime('+365 days',$todays_date));

// must use the date_create(); function
$int = date_diff(date_create($todays_date), date_create($exp));

// result, Time difference in days.
echo " time difference"." ".$int->format('%a'); 
?>
Antoine
  • 1,393
  • 4
  • 20
  • 26
Misha Lin
  • 21
  • 3