|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-05-04 16:54 UTC] giorgio dot liscio at email dot it
Description:
------------
hi,
$a = \DateInterval::createFromDateString("1 month 1 hour");
echo $a->h; // ok
$a = \DateInterval::createFromDateString("1 month 0 hour");
echo $a->h; // ok
$a = \DateInterval::createFromDateString("1 month -1 hour");
echo $a->h; // fatal error, ->h is not defined
Fatal error: main(): Unknown property (h) in ...index.php on line 30
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 15 20:00:02 2025 UTC |
please read carefully because DateInterval is really ambiguous since it is just introduced in php5.3 can be modified before lot of users starts to use this lib what is ambiguous: i can do, for example: $datetimeobj->diff($datetimeobj2, FALSE); to obtain a dateinterval with negative values ( read: $obj->h = -1 ) but i can't do DateInterval::createFromDateString("-1 hour"); i don't know how ambiguity will be resolved, but my suggests are: SOLUTION 1 { - drop ->invert and allow negative values to ->y ->m ->d ->h ->i ->s ->diff() second parameter works good now - add a method to dateInterval to invert all values ( ->invertAll() ) that not sets ->invert, but inverts single properties ( $this->h = $this->h * -1; ) - modify constructor to match the new logic iso8601 seems do not allow negative single values, but can be good to have a constructor like this: public DateInterval::__construct( string $interval_spec , bool $invertAllValues) that converts P2Y4DT6H8M in an hypothetically P -2Y -4D T -6H -8M - drop ->add and ->sub from DateTime class and implement ->change that offers same functionality and more when used with negative intervals and positive intervals } SOLUTION 2 { DateInterval properties ymdhis should be always >=0 - DateTime->diff() should not never returns an object with negative values, but should flag inverted intervals using ->invert = TRUE - drop ->add and ->sub and implement ->change that offers same functionality $a = DateInterval("P1Y"); $datetime->change($a); // add $a->invert = true; $datetime->change($a) // subtract } i sincerely prefer the first version because mixing negative and positive single values would be nice (DateIntervall::createFromDateString("1 year 1 month -10 days")) but i will like any solution that resolves the ambiguities of this class