DateIntervalクラスは、日付算術のこの種のために最適です。 DateTime :: add、DateTime :: subtract、およびDateTime :: diffを使用してそれらを操作できます。
<?php
$types = array(
'imac' => array ('2002-10-10', '2003-11-22', '2004-11-10'),
'iphone' => array ('2007-09-11', '2008-05-12', '2009-06-14', '2010-06-05'),
);
$typeIntervals = array();
$typeAverage = array();
foreach ($types as $type=>$dates) {
$last = null;
$typeIntervals[$type] = array();
foreach ($dates as $date) {
$current = new DateTime($date);
if ($last) {
$interval = $current->diff($last);
$typeIntervals[$type][] = $interval->days;
}
$last = $current;
}
$typeAverage[$type] = array_sum($typeIntervals[$type])/count($typeIntervals[$type]);
}
print_r(typeIntervals);
print_r($typeAverage);
この意志の出力は: "はい、それが可能だ":
Array (
[imac] => Array (
[0] => 408
[1] => 354
)
[iphone] => Array (
[0] => 244
[1] => 398
[2] => 356
)
)
Array (
[imac] => 381
[iphone] => 332.66666666667
)
あなたはより具体的な答えをお探しですか? –