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?
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 )
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.
The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With