php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54660 DateInterval Fatal error: main(): Unknown property (h)
Submitted: 2011-05-04 16:54 UTC Modified: 2011-06-05 03:42 UTC
From: giorgio dot liscio at email dot it Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.6 OS: any?
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: giorgio dot liscio at email dot it
New email:
PHP Version: OS:

 

 [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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-04 17:19 UTC] derick@php.net
-Status: Open +Status: Feedback
 [2011-05-04 17:19 UTC] derick@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 17:50 UTC] giorgio dot liscio at email dot it
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
 [2011-05-04 17:51 UTC] giorgio dot liscio at email dot it
hi derick, thank you, i will try! read my suggests anyway!!
 [2011-05-04 17:53 UTC] giorgio dot liscio at email dot it
-Status: Feedback +Status: Open
 [2011-05-04 17:53 UTC] giorgio dot liscio at email dot it
reopened feedback provided
 [2011-05-04 21:16 UTC] giorgio dot liscio at email dot it
update: on latest trunk first snippet work good (-1 is obtained)
 [2011-06-05 03:42 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2011-06-05 03:42 UTC] felipe@php.net
It's working fine in 5.3, 5.4 and trunk.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 21:01:33 2025 UTC