A date diff function for PHP before version 5.3.0

Before PHP 5.3.0 there were no date_diff(), then this function can be useful. It returns the difference in days, and you can modify it so it returns hours and seconds as a string i have borrowed it from http://php.net/manual/en/function.date-diff.php. I might some day clean up this function.

<?php
function cc2_date_diff($start, $end) {
   
       
$sdate = strtotime($start);
       
$edate = strtotime($end);
       
        if (
$edate < $sdate) {
           
$sdate_temp = $sdate;
           
$sdate = $edate;
           
$edate = $sdate_temp;
           
        }
       
$time = $edate - $sdate;
       
$preday[0] = 0;
       
        if(
$time>=0 && $time<=59) {
               
// Seconds
               
$timeshift = $time.' seconds ';

        } elseif(
$time>=60 && $time<=3599) {
               
// Minutes + Seconds
               
$pmin = ($edate - $sdate) / 60;
               
$premin = explode('.', $pmin);
              
               
$presec = $pmin-$premin[0];
               
$sec = $presec*60;
              
               
$timeshift = $premin[0].' min '.round($sec,0).' sec ';

        } elseif(
$time>=3600 && $time<=86399) {
               
// Hours + Minutes
               
$phour = ($edate - $sdate) / 3600;
               
$prehour = explode('.',$phour);
              
               
$premin = $phour-$prehour[0];
               
$min = explode('.',$premin*60);
              
               
$presec = '0.'.$min[1];
               
$sec = $presec*60;

               
$timeshift = $prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec ';

        } elseif(
$time>=86400) {
               
// Days + Hours + Minutes
               
$pday = ($edate - $sdate) / 86400;
               
$preday = explode('.',$pday);

               
$phour = $pday-$preday[0];
               
$prehour = explode('.',$phour*24);

               
$premin = ($phour*24)-$prehour[0];
               
$min = explode('.',$premin*60);
              
               
$presec = '0.'.$min[1];
               
$sec = $presec*60;
              
               
$timeshift = $preday[0].' days '.$prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec ';

        }
       
        return
$preday[0];
}
?>
Knowledge keywords: