PHP date difference

I have the following code:

$dStart = new DateTime('2013-03-15'); $dEnd = new DateTime('2013-04-01'); $dDiff = $dStart->diff($dEnd); echo $dDiff->days; 

I do not know why I get 6015 as a result.

+7
source share
5 answers

Try

 $dStart = strtotime('2013-03-15'); $dEnd = strtotime('2013-04-01'); $dDiff = $dEnd - $dStart; echo date('H:i:s',$dDiff); 

or according to your code try

 $dDiff = $dStart->diff($dEnd); $date->format('d',$dDiff); echo $dDiff->days; 

if you want diff in days tried with this also

 echo floor($dDiff/(60*60*24)); 
+4
source

Try it -

 $dStart = new DateTime('2013-03-15'); $dEnd = new DateTime('2013-04-01'); $dDiff = $dStart->diff($dEnd); echo $dDiff->format('%d days') 

Check PHP

Please check the demo link

+3
source

use this

  $datetime1 = date_create('2013-03-15'); $datetime2 = date_create('2013-04-01'); $interval = date_diff($datetime1, $datetime2); echo $interval->format('%R%a days'); 
+1
source

I prefer something like:

 function days_diff($first_date, $second_date) { $later = new DateTime($second_date); $then = new DateTime($first_date); return $later->diff($then)->format('a'); } 
+1
source

I got the same 6015 days in PHP 5.3.0 and found a solution using var_dump() . My exact code is here:

 $timestring = "Thu, 13 Jun 2013 14:05:59 GMT"; date_default_timezone_set('GMT'); $date = DateTime::createFromFormat('D, d MYG:i:s T', $timeString); $nowdate = new DateTime("now"); $interval = $date->diff($nowdate); 

Now, if I do a var_dump($interval) , the result is:

 object(DateInterval)#5 (8) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(19) ["i"]=> int(45) ["s"]=> int(33) ["invert"]=> int(0) ["days"]=> int(6015) } 

So, hours ( h ), minutes ( i ) and seconds ( s ) are set correctly, but there is another days property that remains constant at block 6015, and this is what others get as an error. Well, I can’t understand where he gets this value. Again, according to the PHP manual for DateInterval at http://www.php.net/manual/en/class.dateinterval.php , I tried to access them as object properties, and everything went fine.

Therefore, I get the exact result:

 echo (string) $interval->d." days ago"; 
0
source

All Articles