php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65654 Extending DateInterval and unable to use $this->days
Submitted: 2013-09-11 13:26 UTC Modified: 2022-05-13 13:56 UTC
From: steven at gowebprint dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.4.19 OS: Cent OS 6.4
Private report: No CVE-ID: None
 [2013-09-11 13:26 UTC] steven at gowebprint dot com
Description:
------------
I am trying to extend DateTime and DaveInterval to add extra functionality to the core classes, but I have been unable to write to $this->days internally to the object.

As a side note, I have been able to cause a seg fault within our application when writing to $interval->days = 100 externally, but unable to reproduce on a small script.

Test script:
---------------
http://pastebin.com/tBay704K

Expected result:
----------------
object(DateInterval)[3]
  public 'y' => int 1
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'days' => int 365
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0
object(test\DateInterval)[4]
  public 'days' => int 365
  public 'y' => int 1
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

Actual result:
--------------
object(DateInterval)[3]
  public 'y' => int 1
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'days' => int 365
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0
object(test\DateInterval)[4]
  public 'days' => boolean false
  public 'y' => int 1
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-11 13:36 UTC] derick@php.net
"I am trying to extend DateTime and DaveInterval to add extra functionality to the core classes, but I have been unable to write to $this->days internally to the object."

That's not really a bug. "days" is a calculated version that only make sense in the context of having compared two other DateTime objects. Without those other two objects presents, its value makes no sense.
 [2013-09-11 14:00 UTC] steven at gowebprint dot com
You are right.

I have extended DateTime object and overrides the diff method with

public function diff ($datetime2 , $absolute = false){
    return new DateInterval(parent::diff($datetime2,$absolute));
}

If you look at the sample code that would be return the DateInterval in the test namespace.

Since I am overriding the diff method I want to return a DateInterval object that behaves like it would from \DateTime which includes a populated days property , but I have no way of setting the days property or telling \DateTime to return a different DateInterval object, so I am unable to return a DateInteral object with a days property filled and returned from the diff method as expected.

The manual says:

If the DateInterval object was created by DateTime::diff(), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.

This is the method I have overridden and I am trying to return a extended DateInterval object, whilst keeping the all of the DateInterval property's intact including days.
 [2022-05-13 13:56 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2022-05-13 13:56 UTC] derick@php.net
The linked script is no longer available. If this is still a pressing issue for you, please file a new ticket at https://github.com/php/php-src/issues.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 13:01:29 2024 UTC