PHP Doku:: Returns the difference between two DateTime objects - datetime.diff.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatums- und zeitrelevante ErweiterungenDatum und UhrzeitThe DateTime classDateTime::diff

Ein Service von Reinhard Neidl - Webprogrammierung.

The DateTime class

<<DateTime::createFromFormat

DateTime::format>>

DateTime::diff

(PHP 5 >= 5.3.0)

DateTime::diffReturns the difference between two DateTime objects

Beschreibung

Objektorientierter Stil

public DateInterval DateTime::diff ( DateTime $datetime2 [, bool $absolute = false ] )

Prozeduraler Stil

DateInterval date_diff ( DateTime $datetime1 , DateTime $datetime2 [, bool $absolute = false ] )

Returns the difference between two DateTime objects.

Parameter-Liste

datetime

The date to compare to.

absolute

Whether to return absolute difference.

Rückgabewerte

The DateInterval object representing the difference between the two datesIm Fehlerfall wird FALSE zurückgegeben..

Beispiele

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)

Siehe auch

  • DateInterval::format() - Formats the interval
  • DateTime::add() - Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
  • DateTime::sub() - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object

4 BenutzerBeiträge:
- Beiträge aktualisieren...
Umair Ashraf
22.11.2010 8:49
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"));
}
?>
acrion at gmail dot com
13.05.2010 17:52
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;
}
?>
Dennis C
10.05.2010 8:24
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 \>");
?>
Anonymous
4.08.2009 14:17
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)



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)