如何在 DateInterval 扩展 class 中创建日期 属性?
How can I create the days property in a DateInterval extension class?
我想扩展 DateInterval
class 以添加我自己的方法。为此,我想从 DateInterval
对象创建 DateIntervalEx
扩展 class 的实例。
示例:
$dateInterval = date_create('yesterday')->diff(date_create('today 13:24'));
$diEx = new DateIntervalEx($dateInterval);
我对 class 的尝试:
class DateIntervalEx extends Dateinterval{
public function __construct(DateInterval $interval){
parent::__construct('P0D');
foreach($interval as $prop => $value){
$this->$prop = $value;
}
}
}
diff()
方法returns一个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,
))
但是我的分机 class returns 天 => 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,
))
如何将天数 属性 设置为正确的值?
无法在 PHP 上执行。
如果 DateInterval
是使用 DateTime::diff()
创建的,$days
属性 由 PHP 运行时设置,正如您可能在 docs:
days
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.
Before PHP 5.4.20/5.5.4 instead of FALSE you will receive -99999 upon accessing the property.
检查两个重要的 DateTime
扩展项目如何处理这个问题(例如,他们没有):
Chronos(ChronosInterval
扩展 DateTimeInterval
):
/**
* Create a ChronosInterval instance from a DateInterval one. Can not instance
* DateInterval objects created from DateTime::diff() as you can't externally
* set the $days field.
*
* @param \DateInterval $di The DateInterval instance to copy.
* @throws \InvalidArgumentException
* @return static
*/
public static function instance(DateInterval $di): self
{
if (static::wasCreatedFromDiff($di)) {
throw new InvalidArgumentException(
"Can not instance a DateInterval object created from DateTime::diff()."
);
}
// ...
碳(CarbonInterval
从 DateTimeInterval
延伸)
/**
* Create a CarbonInterval instance from a DateInterval one. Can not instance
* DateInterval objects created from DateTime::diff() as you can't externally
* set the $days field.
*
* @param DateInterval $interval
*
* @return static
*/
public static function instance(DateInterval $interval)
{
return self::castIntervalToClass($interval, static::class);
}
你可以查看相关的C代码here。
我想扩展 DateInterval
class 以添加我自己的方法。为此,我想从 DateInterval
对象创建 DateIntervalEx
扩展 class 的实例。
示例:
$dateInterval = date_create('yesterday')->diff(date_create('today 13:24'));
$diEx = new DateIntervalEx($dateInterval);
我对 class 的尝试:
class DateIntervalEx extends Dateinterval{
public function __construct(DateInterval $interval){
parent::__construct('P0D');
foreach($interval as $prop => $value){
$this->$prop = $value;
}
}
}
diff()
方法returns一个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,
))
但是我的分机 class returns 天 => 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,
))
如何将天数 属性 设置为正确的值?
无法在 PHP 上执行。
如果 DateInterval
是使用 DateTime::diff()
创建的,$days
属性 由 PHP 运行时设置,正如您可能在 docs:
days
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.
Before PHP 5.4.20/5.5.4 instead of FALSE you will receive -99999 upon accessing the property.
检查两个重要的 DateTime
扩展项目如何处理这个问题(例如,他们没有):
Chronos(ChronosInterval
扩展 DateTimeInterval
):
/** * Create a ChronosInterval instance from a DateInterval one. Can not instance * DateInterval objects created from DateTime::diff() as you can't externally * set the $days field. * * @param \DateInterval $di The DateInterval instance to copy. * @throws \InvalidArgumentException * @return static */ public static function instance(DateInterval $di): self { if (static::wasCreatedFromDiff($di)) { throw new InvalidArgumentException( "Can not instance a DateInterval object created from DateTime::diff()." ); } // ...
碳(CarbonInterval
从 DateTimeInterval
延伸)
/** * Create a CarbonInterval instance from a DateInterval one. Can not instance * DateInterval objects created from DateTime::diff() as you can't externally * set the $days field. * * @param DateInterval $interval * * @return static */ public static function instance(DateInterval $interval) { return self::castIntervalToClass($interval, static::class); }
你可以查看相关的C代码here。