The DateInterval format unfortunately leads to inconsitencies:
<?php
$start = new DateTime('2009-01-01 00:00:00'); // 31 days
$time_span = $start->diff(new DateTime('2009-02-01 00:00:00'));
var_dump($time_span); // returns '1 month'
$start = new DateTime('2009-02-01 00:00:00'); //28 days
$time_span = $start->diff(new DateTime('2009-03-01 00:00:01'));
var_dump($time_span); // returns '1 month'
?>
Of course it is possible to guess what does DateTime mean for a month, but in such case the invoking program should take care of month lengths and leap years (imagine counting days between dates years appart) - DateTime class is supposed to do that. In my opinion this function should return a value in seconds (or any other stable unit). Afterwards the program would have to convert the value as needed, but the value would be exact.
Description
Retourne la différence entre deux objets DateTime.
Liste de paramètres
- datetime
-
La date de comparaison.
- absolute
-
S'il faut retourner une différence absolue. Par défaut, FALSE.
Valeurs de retour
La différence entre les deux dates.
DateTime::diff
andarin2 at yahoo dot com
18-Nov-2009 10:32
18-Nov-2009 10:32
Anonymous
04-Aug-2009 12:17
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
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;
?>
