我遇到了同样的问题(还有其他一些问题),因此创建了一个类,以便能够添加和减去DateInterval。它还支持负的ISO8601日期间隔(例如“P-2M1DT3H-56M21S”)。
以下是代码(欢迎提出建议,我是PHP方面的初学者):
class MyDateInterval extends DateInterval
{
public function __construct($interval_spec)
{
$interval_spec = str_replace('+', '', $interval_spec);
$pos = strpos($interval_spec, '-');
if ($pos !== false) {
$pattern = '/P(?P<ymd>(?P<years>-?\d+Y)?(?P<months>-?\d+M)?(?P<days>-?\d+D)?)?(?P<hms>T(?P<hours>-?\d+H)?(?P<minutes>-?\d+M)?(?P<seconds>-?\d+S)?)?/';
$match = preg_match($pattern, $interval_spec, $matches);
$group_names = array('years', 'months', 'days', 'hours', 'minutes', 'seconds');
$negative_parts = array();
$positive_parts = array();
$all_negative = true;
foreach ($matches as $k => $v) {
if (in_array($k, $group_names, true)) {
if (substr($v, 0, 1) == '-' and $v != '')
$negative_parts[$k] = $v;
if (substr($v, 0, 1) != '-' and $v != '')
$positive_parts[$k] = $v;
}
}
if (count($positive_parts) == 0) {
$interval_spec = str_replace('-', '', $interval_spec);
parent::__construct($interval_spec);
$this->invert = 1;
} else {
$negative_interval_spec = 'P';
$positive_interval_spec = 'P';
if ($matches['ymd'] != '') {
foreach ($matches as $k => $v) {
if (in_array($k, array_slice($group_names, 0, 3))) {
$negative_interval_spec .= $negative_parts[$k];
$positive_interval_spec .= $positive_parts[$k];
}
}
}
if ($matches['hms'] != '') {
$negative_ok = false;
$positive_ok = false;
foreach ($matches as $k => $v) {
if (in_array($k, array_slice($group_names, 3, 3))) {
if ($negative_parts[$k] != '' and ! $negative_ok) {
$negative_interval_spec .= 'T';
$negative_ok = true;
}
$negative_interval_spec .= $negative_parts[$k];
if ($positive_parts[$k] != '' and ! $positive_ok) {
$positive_interval_spec .= 'T';
$positive_ok = true;
}
$positive_interval_spec .= $positive_parts[$k];
}
}
}
$negative_interval_spec = str_replace('-', '', $negative_interval_spec);
$from = new DateTime('2013-01-01');
$to = new DateTime('2013-01-01');
$to = $to->add(new DateInterval($positive_interval_spec));
$to = $to->sub(new DateInterval($negative_interval_spec));
$diff = $from->diff($to);
parent::__construct($diff->format('P%yY%mM%dDT%hH%iM%sS'));
$this->invert = $diff->invert;
}
} else {
parent::__construct($interval_spec);
}
}
public static function fromDateInterval(DateInterval $from)
{
return new MyDateInterval($from->format('P%yY%mM%dDT%hH%iM%sS'));
}
public static function fromSeconds($from)
{
$invert = false;
if ($from < 0)
$invert = true;
$from = abs($from);
$years = floor($from / (365 * 30 * 24 * 60 * 60));
$from = $from % (365 * 30 * 24 * 60 * 60);
$months = floor($from / (30 * 24 * 60 * 60));
$from = $from % (30 * 24 * 60 * 60);
$days = floor($from / (24 * 60 * 60));
$from = $from % (24 * 60 * 60);
$hours = floor($from / (60 * 60));
$from = $from % (60 * 60);
$minutes = floor($from / 60);
$seconds = floor($from % 60);
if ($invert)
return new MyDateInterval(sprintf("P-%dY-%dM-%dDT-%dH-%dM-%dS", $years, $months, $days, $hours, $minutes, $seconds));
return new MyDateInterval(sprintf("P%dY%dM%dDT%dH%dM%dS", $years, $months, $days, $hours, $minutes, $seconds));
}
public function to_seconds()
{
$seconds = ($this->y * 365 * 24 * 60 * 60)
+ ($this->m * 30 * 24 * 60 * 60)
+ ($this->d * 24 * 60 * 60)
+ ($this->h * 60 * 60)
+ ($this->i * 60)
+ $this->s;
if ($this->invert == 1)
return $seconds * -1;
return $seconds;
}
public function to_hours()
{
$hours = round($this->to_seconds() / (60 * 60), 2);
return $hours;
}
public function add($interval)
{
$sum = $this->to_seconds() + $interval->to_seconds();
$new = MyDateInterval::fromSeconds($sum);
foreach ($new as $k => $v) $this->$k = $v;
return $this;
}
public function sub($interval)
{
$diff = $this->to_seconds() - $interval->to_seconds();
$new = MyDateInterval::fromSeconds($diff);
foreach ($new as $k => $v) $this->$k = $v;
return $this;
}
public function recalculate()
{
$seconds = $this->to_seconds();
$new = MyDateInterval::fromSeconds($seconds);
foreach ($new as $k => $v) $this->$k = $v;
return $this;
}
}
$ php -r '$di = new DateInterval("P1Y");$di->invert = 1; var_dump($di); echo $di->format("%r%y years");'
它会输出“-1 years”,这是正确的输出结果。你是如何得到invert=1
后的“positive”结果的呢? - hijariancreateFromDateString
也可以工作。我认为这比使用构造函数并通过invert
内部属性进行操作更加简洁。 - hijarian