(PHP 5 >= 5.3.0)
DateTime::diff — Returns the difference between two DateTime objects
Objektorientierter Stil
Prozeduraler Stil
Returns the difference between two DateTime objects.
The date to compare to.
Whether to return absolute difference.
The DateInterval object representing the difference between the two datesIm Fehlerfall wird FALSE zurückgegeben..
Beispiel #1 DateTime::diff() example
Objektorientierter Stil
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days');
?>
Prozeduraler Stil
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
?>
The above examples will output:
+2 days
Beispiel #2 DateTime object comparison
Hinweis:
As of PHP 5.2.2, DateTime objects can be compared using comparison operators.
<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 == $date2);
var_dump($date1 < $date2);
var_dump($date1 > $date2);
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
bool(false) bool(true) bool(false)
for PHP version 5.3
<?php
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) return pluralize( $interval->y, 'year' ) . $suffix;
if ( $v = $interval->m >= 1 ) return pluralize( $interval->m, 'month' ) . $suffix;
if ( $v = $interval->d >= 1 ) return pluralize( $interval->d, 'day' ) . $suffix;
if ( $v = $interval->h >= 1 ) return pluralize( $interval->h, 'hour' ) . $suffix;
if ( $v = $interval->i >= 1 ) return pluralize( $interval->i, 'minute' ) . $suffix;
return pluralize( $interval->s, 'second' ) . $suffix;
}
?>
for PHP version 5.2
<?php
public function ago($dt)
{
$dt = date_parse($dt);
$now = date_parse(date("Y-m-d H:i:s"));
$suffix = " ago";
if ($now['year'] != $dt['year']) return $this->pluralize($now['year'] - $dt['year'], "year") . $suffix;
if ($now['month'] != $dt['month']) return $this->pluralize($now['month'] - $dt['month'], "month") . $suffix;
if ($now['day'] != $dt['day']) return $this->pluralize($now['day'] - $dt['day'], "day") . $suffix;
if ($now['hour'] != $dt['hour']) return $this->pluralize($now['hour'] - $dt['hour'], "hour") . $suffix;
if ($now['minute'] != $dt['minute']) return $this->pluralize($now['minute'] - $dt['minute'], "minute") . $suffix;
if ($now['second'] != $dt['second']) return $this->pluralize($now['second'] - $dt['second'], "second") . $suffix;
return "just now";
}
private function pluralize($count, $text)
{
return $count . (($count == 1) ? (" $text") : (" ${text}s"));
}
?>
If you want to quickly scan through the resulting intervals, you can use the undocumented properties of DateInterval.
The function below returns a single number of years, months, days, hours, minutes or seconds between the current date and the provided date. If the date occurs in the past (is negative/inverted), it suffixes it with 'ago'.
<?php
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) return pluralize( $interval->y, 'year' ) . $suffix;
if ( $v = $interval->m >= 1 ) return pluralize( $interval->m, 'month' ) . $suffix;
if ( $v = $interval->d >= 1 ) return pluralize( $interval->d, 'day' ) . $suffix;
if ( $v = $interval->h >= 1 ) return pluralize( $interval->h, 'hour' ) . $suffix;
if ( $v = $interval->i >= 1 ) return pluralize( $interval->i, 'minute' ) . $suffix;
return pluralize( $interval->s, 'second' ) . $suffix;
}
?>
For those like me who don't yet have PHP 5.3 installed on their host, here's a simple alternative to get the number of days between two dates in the format '2010-3-23' or similar acceptable to strtotime(). You need PHP 5.2.
<?php
function date_diff($date1, $date2) {
$current = $date1;
$datetime2 = date_create($date2);
$count = 0;
while(date_create($current) < $datetime2){
$current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
$count++;
}
return $count;
}
echo (date_diff('2010-3-9', '2011-4-10')." days <br \>");
?>
You don't need to calculate the exact difference if you just want to know what date comes earlier:
<?php
date_default_timezone_set('Europe/Madrid');
$d1 = new DateTime('1492-01-01');
$d2 = new DateTime('1492-12-31');
var_dump($d1 < $d2);
var_dump($d1 > $d2);
var_dump($d1 == $d2);
?>
bool(true)
bool(false)
bool(false)