Home / code

Date difference

Writer: Christophe Wolfs
Published: 27 March 2008

This a small fast function to get the difference from 2 dates in days, hours, minutes and seconds.
Thanks to Matthijs.

<?php
function datediff($date1,$date2)
{
  $s=strtotime($date2) - strtotime($date1);

  $m=intval($s/60);
  $s=$s%60;

  $h=intval($m/60);
  $m=$m%60;

  $d=intval($h/24);
  $h=$h%24;

  $diff=""; 
  if(!$d=="0") $diff.="$d days ";
  if(!$h=="0") $diff.="$h hours ";
  if(!$m=="0") $diff.="$m minutes ";
  if(!$s=="0") $diff.="$s seconds ";
  if($diff=="") $diff="-";
  trim($diff); 
  return $diff;
}
?>

 

TOP