public function average_time($type = 3){
$formatStyle = [
1 => 'Y-m-d H:i:s',
2 => 'Y-m-d H:i',
3 => 'Y-m-d',
];
$startTime = strtotime("2021-01-01 00:00:00");
$endTime = strtotime("+1 day",$startTime);
$format = $formatStyle[$type];
if ($type == 1) {
$seconds = 60;
} elseif ($type == 2) {
$seconds = 3600;
} else {
$seconds = 86400;
}
$result = [];
$for = intval(($endTime - $startTime) / $seconds);
for($i = 0; $i <= $for; $i++){
$result[date($format,$startTime + $i * $seconds)] = 'somethings';
}
print_r($result);
}
Array
(
[2021-01-01 00:00] => somethings
[2021-01-01 01:00] => somethings
[2021-01-01 02:00] => somethings
[2021-01-01 03:00] => somethings
[2021-01-01 04:00] => somethings
[2021-01-01 05:00] => somethings
[2021-01-01 06:00] => somethings
[2021-01-01 07:00] => somethings
[2021-01-01 08:00] => somethings
[2021-01-01 09:00] => somethings
[2021-01-01 10:00] => somethings
[2021-01-01 11:00] => somethings
[2021-01-01 12:00] => somethings
[2021-01-01 13:00] => somethings
[2021-01-01 14:00] => somethings
[2021-01-01 15:00] => somethings
[2021-01-01 16:00] => somethings
[2021-01-01 17:00] => somethings
[2021-01-01 18:00] => somethings
[2021-01-01 19:00] => somethings
[2021-01-01 20:00] => somethings
[2021-01-01 21:00] => somethings
[2021-01-01 22:00] => somethings
[2021-01-01 23:00] => somethings
[2021-01-02 00:00] => somethings
)
|