Here is an incomplete workaround for PHP < 5.3.0
<?php
/**
* Workaround for PHP < 5.3.0
*/
if(!function_exists('date_diff')) {
class DateInterval {
public $y;
public $m;
public $d;
public $h;
public $i;
public $s;
public $invert;
public function format($format) {
$format = str_replace('%R%y', ($this->invert ? '-' : '+') . $this->y, $format);
$format = str_replace('%R%m', ($this->invert ? '-' : '+') . $this->m, $format);
$format = str_replace('%R%d', ($this->invert ? '-' : '+') . $this->d, $format);
$format = str_replace('%R%h', ($this->invert ? '-' : '+') . $this->h, $format);
$format = str_replace('%R%i', ($this->invert ? '-' : '+') . $this->i, $format);
$format = str_replace('%R%s', ($this->invert ? '-' : '+') . $this->s, $format);
$format = str_replace('%y', $this->y, $format);
$format = str_replace('%m', $this->m, $format);
$format = str_replace('%d', $this->d, $format);
$format = str_replace('%h', $this->h, $format);
$format = str_replace('%i', $this->i, $format);
$format = str_replace('%s', $this->s, $format);
return $format;
}
}
function date_diff(DateTime $date1, DateTime $date2) {
$diff = new DateInterval();
if($date1 > $date2) {
$tmp = $date1;
$date1 = $date2;
$date2 = $tmp;
$diff->invert = true;
}
$diff->y = ((int) $date2->format('Y')) - ((int) $date1->format('Y'));
$diff->m = ((int) $date2->format('n')) - ((int) $date1->format('n'));
if($diff->m < 0) {
$diff->y -= 1;
$diff->m = $diff->m + 12;
}
$diff->d = ((int) $date2->format('j')) - ((int) $date1->format('j'));
if($diff->d < 0) {
$diff->m -= 1;
$diff->d = $diff->d + ((int) $date1->format('t'));
}
$diff->h = ((int) $date2->format('G')) - ((int) $date1->format('G'));
if($diff->h < 0) {
$diff->d -= 1;
$diff->h = $diff->h + 24;
}
$diff->i = ((int) $date2->format('i')) - ((int) $date1->format('i'));
if($diff->i < 0) {
$diff->h -= 1;
$diff->i = $diff->i + 60;
}
$diff->s = ((int) $date2->format('s')) - ((int) $date1->format('s'));
if($diff->s < 0) {
$diff->i -= 1;
$diff->s = $diff->s + 60;
}
return $diff;
}
}
?>
Description
This function is an alias of: DateTime::diff()
date_diff
Sven Obser
16-Sep-2010 09:40
16-Sep-2010 09:40
Sergio Abreu
26-Jun-2010 04:22
26-Jun-2010 04: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
27-May-2010 02:37
27-May-2010 02: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 08:58
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
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
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;
}
?>
