note 100574 added to class.dateinterval

From: Date: Sun, 24 Oct 2010 20:49:10 +0000
Subject: note 100574 added to class.dateinterval
Groups: php.notes 
Request: Send a blank email to php-notes+get-173418@lists.php.net to get a copy of this message
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. ---- Server IP: 82.119.225.41 Probable Submitter: 85.135.149.211 ---- Manual Page -- http://www.php.net/manual/en/class.dateinterval.php Edit -- https://master.php.net/note/edit/100574 Del: integrated -- https://master.php.net/note/delete/100574/integrated Del: useless -- https://master.php.net/note/delete/100574/useless Del: bad code -- https://master.php.net/note/delete/100574/bad+code Del: spam -- https://master.php.net/note/delete/100574/spam Del: non-english -- https://master.php.net/note/delete/100574/non-english Del: in docs -- https://master.php.net/note/delete/100574/in+docs Del: other reasons-- https://master.php.net/note/delete/100574 Reject -- https://master.php.net/note/reject/100574 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173418) next »