downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

date_format> <date_default_timezone_set
Last updated: Fri, 12 Mar 2010

view this page in

date_diff

(PHP 5 >= 5.3.0)

date_diffAlias of DateTime::diff

Description

This function is an alias of: DateTime::diff



date_format> <date_default_timezone_set
Last updated: Fri, 12 Mar 2010
 
add a note add a note User Contributed Notes
date_diff
eugene at ultimatecms dot co dot za
17-Mar-2010 08: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 02: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 05: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, 12 Mar 2010
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites