Edit report at http://bugs.php.net/bug.php?id=54660&edit=1

 ID:                 54660
 Comment by:         giorgio dot liscio at email dot it
 Reported by:        giorgio dot liscio at email dot it
 Summary:            DateInterval Fatal error: main(): Unknown property
                     (h)
 Status:             Feedback
 Type:               Bug
 Package:            Date/time related
 Operating System:   any?
 PHP Version:        5.3.6
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2011-05-04 17:19:09] der...@php.net

Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/

This works fine for me:



derick@whisky:~$ php -v

PHP 5.3.7-dev (cli) (built: May  4 2011 09:16:17) (DEBUG)



<?php

$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

?>

10-1

------------------------------------------------------------------------
[2011-05-04 16:54:49] 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



------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=54660&edit=1

Reply via email to