date_format> <date_default_timezone_set
Last updated: Fri, 16 Jul 2010

date_diff

(PHP 5 >= 5.3.0)

date_diffSinônimo de DateTime::diff

Descrição

Esta função é um apelido para: DateTime::diff



date_format> <date_default_timezone_set
Last updated: Fri, 16 Jul 2010
 
User Contributed Notes
date_diff
Sergio Abreu
26-Jun-2010 11:22
<?php
/*
 * A mathematical decimal difference between two informed dates
 *
 * Author: Sergio Abreu
 * Website: http://sites.sitesbr.net
 *
 * Features:
 * Automatic conversion on dates informed as string.
 * Possibility of absolute values (always +) or relative (-/+)
*/

function s_datediff( $str_interval, $dt_menor, $dt_maior, $relative=false){

       if(
is_string( $dt_menor)) $dt_menor = date_create( $dt_menor);
       if(
is_string( $dt_maior)) $dt_maior = date_create( $dt_maior);

      
$diff = date_diff( $dt_menor, $dt_maior, ! $relative);
      
       switch(
$str_interval){
           case
"y":
              
$total = $diff->y + $diff->m / 12 + $diff->d / 365.25; break;
           case
"m":
              
$total= $diff->y * 12 + $diff->m + $diff->d/30 + $diff->h / 24;
               break;
           case
"d":
              
$total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24 + $diff->i / 60;
               break;
           case
"h":
              
$total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i/60;
               break;
           case
"i":
              
$total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s/60;
               break;
           case
"s":
              
$total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i)*60 + $diff->s;
               break;
          }
       if(
$diff->invert)
               return -
1 * $total;
       else    return
$total;
   }

/* Enjoy and feedback me ;-) */
?>
Flavio Tubino
26-May-2010 09:37
This is a very simple function to calculate the difference between two datetime values, returning the result in seconds. To convert to minutes, just divide the result by 60. In hours, by 3600 and so on.

Enjoy.

<?php
function time_diff($dt1,$dt2){
   
$y1 = substr($dt1,0,4);
   
$m1 = substr($dt1,5,2);
   
$d1 = substr($dt1,8,2);
   
$h1 = substr($dt1,11,2);
   
$i1 = substr($dt1,14,2);
   
$s1 = substr($dt1,17,2);   

   
$y2 = substr($dt2,0,4);
   
$m2 = substr($dt2,5,2);
   
$d2 = substr($dt2,8,2);
   
$h2 = substr($dt2,11,2);
   
$i2 = substr($dt2,14,2);
   
$s2 = substr($dt2,17,2);   

   
$r1=date('U',mktime($h1,$i1,$s1,$m1,$d1,$y1));
   
$r2=date('U',mktime($h2,$i2,$s2,$m2,$d2,$y2));
    return (
$r1-$r2);

}
?>
eugene at ultimatecms dot co dot za
17-Mar-2010 04:58
This function calculates the difference up to the second.

<?php

function date_diff($start, $end="NOW")
{
       
$sdate = strtotime($start);
       
$edate = strtotime($end);

       
$time = $edate - $sdate;
        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
$timeshift;
}

// EXAMPLE:

$start_date = 2010-03-15 13:00:00
$end_date
= 2010-03-17 09:36:15

echo date_diff($start_date, $end_date);

?>

Returns: 1days 20hours 36min 15sec
Can be taken up to centuries - if you do the calculations.

Hope this finally helps someone! :D
mark at dynom dot nl
02-Jul-2009 09:44
If you simply want to compare two dates, using conditional operators works too:

<?php
$start
= new DateTime('08-06-1995 Europe/Copenhagen'); // DD-MM-YYYY
$end = new DateTime('22-11-1968 Europe/Amsterdam');

if (
$start < $end) {
    echo
"Correct order";
} else {
    echo
"Incorrect order, end date is before the starting date";
}
?>
tom at knapp2meter dot tk
16-Apr-2009 12:06
A simple way to get the time lag (format: <hours>.<one-hundredth of one hour>).

Hier ein einfacher Weg zur Bestimmung der Zeitdifferenz (Format: <Stunden>.<hundertstel Stunde>).

<?php

function GetDeltaTime($dtTime1, $dtTime2)
{
 
$nUXDate1 = strtotime($dtTime1->format("Y-m-d H:i:s"));
 
$nUXDate2 = strtotime($dtTime2->format("Y-m-d H:i:s"));

 
$nUXDelta = $nUXDate1 - $nUXDate2;
 
$strDeltaTime = "" . $nUXDelta/60/60; // sec -> hour
           
 
$nPos = strpos($strDeltaTime, ".");
  if (
nPos !== false)
   
$strDeltaTime = substr($strDeltaTime, 0, $nPos + 3);

  return
$strDeltaTime;
}

?>

date_format> <date_default_timezone_set
Last updated: Fri, 16 Jul 2010