1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-01-04 12:00:27 +01:00
openstamanager/modules/statistiche/src/Stats.php
2019-07-11 12:04:22 +02:00

46 lines
1.1 KiB
PHP

<?php
namespace Modules\Statistiche;
use ArrayObject;
use DateTime;
class Stats
{
public static function monthly($original, $start, $end)
{
// Copia dei dati
$array = new ArrayObject($original);
$data = $array->getArrayCopy();
// Ordinamento
array_multisort(array_column($data, 'year'), SORT_ASC,
array_column($data, 'month'), SORT_ASC,
$data);
// Differenza delle date in mesi
$d1 = new DateTime($start);
$d2 = new DateTime($end);
$count = $d1->diff($d2)->m + ($d1->diff($d2)->y * 12) + 1;
$year = $d1->format('Y');
$month = intval($d1->format('m')) - 1;
for ($i = 0; $i < $count; ++$i) {
$year = $month >= 12 ? $year + 1 : $year;
$month = $month % 12;
if (!isset($data[$i]) || intval($data[$i]['month']) != $month + 1) {
array_splice($data, $i, 0, [[
'result' => 0,
'year' => $year,
'month' => $month + 1,
]]);
}
++$month;
}
return $data;
}
}