我希望扩展DateInterval
类以添加自己的方法。为此,我想从DateInterval
对象创建一个DateIntervalEx
扩展类实例。
例如:
$dateInterval = date_create('yesterday')->diff(date_create('today 13:24'));
$diEx = new DateIntervalEx($dateInterval);
我为这门课做出的尝试:
class DateIntervalEx extends Dateinterval{
public function __construct(DateInterval $interval){
parent::__construct('P0D');
foreach($interval as $prop => $value){
$this->$prop = $value;
}
}
}
diff()
方法返回一个DateInterval
对象,其中$days == 1
DateInterval::__set_state(array(
'y' => 0,
'm' => 0,
'd' => 1,
'h' => 13,
'i' => 24,
's' => 0,
'f' => 0.0,
'weekday' => 0,
'weekday_behavior' => 0,
'first_last_day_of' => 0,
'invert' => 0,
'days' => 1,
'special_type' => 0,
'special_amount' => 0,
'have_weekday_relative' => 0,
'have_special_relative' => 0,
))
但是我的扩展类返回的是 days => false。
DateIntervalEx::__set_state(array(
'weekday' => 0,
'weekday_behavior' => 0,
'first_last_day_of' => 0,
'days' => false,
'special_type' => 0,
'special_amount' => 0,
'have_weekday_relative' => 0,
'have_special_relative' => 0,
'y' => 0,
'm' => 0,
'd' => 1,
'h' => 13,
'i' => 24,
's' => 0,
'f' => 0.0,
'invert' => 0,
))
我该如何将days属性设置为正确的值?
parent::__construct($interval->format('P%yY%mM%dDT%hH%iM%sS'));
,结果仍然相同 - 结果对象中的“days”为false。 - CBroe