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

search for in the

DateTime::format> <DateTime::createFromFormat
Last updated: Fri, 06 Nov 2009

view this page in

DateTime::diff

(PHP 5 >= 5.3.0)

DateTime::diffReturns the difference between two DateTime objects

Description

public DateInterval DateTime::diff ( DateTime $datetime [, bool $absolute ] )

Returns the difference between two DateTime objects.

Parameters

datetime

The date to compare to.

absolute

Whether to return absolute difference. Defaults to FALSE.

Return Values

The difference between two dates.



add a note add a note User Contributed Notes
DateTime::diff
Anonymous
04-Aug-2009 12: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)
thinice at gmail dot com
17-Jul-2009 07:29
Some stuff to help you get started:

<?php
// See what's inside
$oDT = new DateTime();
var_dump($oDT);
?>

To do a date difference:

<?php
$oDT
= new DateTime(); //Sets the object to now
$oDTDiff = $oDT->diff(new DateTime('2009-08-18 00:00:01'));
var_dump($oDTDiff);
echo
"Days of difference: ". $oDTDiff->days;
?>

DateTime::format> <DateTime::createFromFormat
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites