note 100574 deleted from class.dateinterval by danbrown
| From: | danbrown@php.net | Date: | Mon, 25 Oct 2010 03:46:59 +0000 |
| Subject: | note 100574 deleted from class.dateinterval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173426@lists.php.net to get a copy of this message | ||
Note Submitter: koniiiik at ksp dot sk
----
Note that the so-called properties of this class (h, m, s, d etc.) are not real OOP properties, only
what PHP folks call properties (real-life attributes, see http://www.php.net/manual/en/language.oop5.properties.php#98267).
That means this class does no normalization at all:
<?php
$interval = new DateInterval('P0D');
$interval->h = -40;
print_r($interval);
?>
will output
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => -40
[i] => 0
[s] => 0
[invert] => 0
[days] =>
)
You might expect it to somehow normalize the interval, like, for example, transform it to something
similar to:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 1
[h] => 16
[i] => 0
[s] => 0
[invert] => 1
[days] =>
)
However, that does not happen, which means even if you do
<?php
echo $interval->format("%R %a days total, %d %H:%M:%S\n");
?>
it will output
+ (unknown) days total, 0 -40:00:00
If you need a class to do this kind of normalization, you'll have to create a child of this one
(and probably include the implementation of the magic methods in the post I linked above).
Luckily, if you want to just subtract a certain number of hours from a DateTime instance, creating
an interval with, say -40 hours and then calling DateTime::add works fine.