You could simple create DateTime
object with timestamp = 0
; then ->add()
all your Dateinterval
's to that object; fetch ->getTimestamp()
and divide it by number intervals added. You will get average number of seconds; example:
function calculateIntervalAverage() {
$offset = new DateTime('@0');
foreach (func_get_args() as $interval) {
$offset->add($interval);
}
return round($offset->getTimestamp() / func_num_args());
}
If you wish to convert seconds to DateInterval
object, you can just create it with PTxS
parameter:
$interval = new DateInterval("PT3600S");
But this will not calculate carry-over-points, like you see on demo. If this is a problem, you can still recalculate carry-over-points; example:
class DateIntervalEnhanced extends DateInterval {
public function recalculate()
{
$from = new DateTime('@0');
$to = clone $from;
$to->add($this);
$diff = $from->diff($to);
foreach ($diff as $k => $v) $this->$k = $v;
return $this;
}
}