Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does absolute parameter do in DateTime::diff(DateTime [, bool absolute = false])

Here is the sample code

$c = new DateTime();
$o = clone $c;
$o->modify('-60 days');
$diff = $c->diff($o);
$diff2 = $c->diff($o, TRUE);
var_dump($diff, $diff2);

which outputs

object(DateInterval)#3 (8) {
  ["y"]=> int(0), ["m"]=> int(1), ["d"]=> int(29), ["h"]=> int(0), ["i"]=> int(0),
  ["s"]=> int(0), ["invert"]=> int(1), ["days"]=> int(60)
}
object(DateInterval)#4 (8) {
  ["y"]=> int(0), ["m"]=> int(1), ["d"]=> int(29), ["h"]=> int(0), ["i"]=> int(0),
  ["s"]=> int(0), ["invert"]=> int(0), ["days"]=> int(60)
}

as I see, only the "invert" property changes. What does this mean?

like image 373
Sudhi Avatar asked Sep 29 '11 11:09

Sudhi


People also ask

What is diff in PHP?

The DateTime::diff() function is an inbuilt function in PHP which is used to return the difference between two given DateTime objects. Syntax: Object oriented style: DateInterval DateTime::diff( DateTimeInterface $datetime2, bool $absolute = FALSE )

How do we use DateTime objects in PHP?

To use the DateTime object you just need to instantiate the the class. $date = new DateTime(); The constructor of this object takes two parameters the first is the time value you want to set the value of the object, you can use a date format, unix timestamp, a day interval or a day period.

What is new DateTime in PHP?

The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format.


1 Answers

The absolute property will return the absolute difference between two DateTime objects. This will change the result to positive when a negative difference is returned.

like image 151
jncraton Avatar answered Oct 14 '22 04:10

jncraton