Answer the question
In order to leave comments, you need to log in
How to group an array of dates by a given interval?
Goodnight.
Tell me which way to look :) There is an array of dates, they need to be grouped according to a given interval (by shifts) from 15:00 on the first day to 15:00 on the second day.
Here is the original data:
Array
(
[0] => 08.12.2014 07:19:27
[1] => 08.12.2014 07:51:49
[2] => 08.12.2014 09:43:45
[3] => 08.12.2014 10:18:52
[4] => 08.12.2014 11:14:12
[5] => 08.12.2014 12:01:24
[6] => 08.12.2014 12:30:09
[7] => 09.12.2014 18:31:59
[8] => 09.12.2014 20:29:13
[9] => 09.12.2014 21:37:42
[10] => 09.12.2014 22:08:58
[11] => 09.12.2014 22:46:20
[12] => 09.12.2014 23:20:22
[13] => 09.12.2014 23:37:20
[14] => 10.12.2014 00:04:14
[15] => 10.12.2014 01:22:13
[16] => 10.12.2014 02:30:16
[17] => 10.12.2014 06:30:40
[18] => 10.12.2014 07:54:43
[19] => 10.12.2014 08:43:03
[20] => 10.12.2014 09:53:54
[21] => 10.12.2014 10:15:49
[22] => 10.12.2014 11:17:59
[23] => 10.12.2014 12:35:01
[24] => 13.12.2014 17:37:46
[25] => 13.12.2014 18:07:49
[26] => 13.12.2014 19:22:28
[27] => 13.12.2014 19:44:46
[28] => 13.12.2014 20:39:15
[29] => 13.12.2014 21:31:40
[30] => 13.12.2014 22:03:00
[31] => 13.12.2014 23:06:12
[32] => 13.12.2014 23:46:17
[33] => 14.12.2014 00:38:47
[34] => 14.12.2014 01:11:12
[35] => 14.12.2014 01:46:53
[36] => 14.12.2014 02:17:10
[37] => 14.12.2014 03:17:12
[38] => 14.12.2014 03:44:56
[39] => 14.12.2014 04:36:19
[40] => 14.12.2014 05:41:39
[41] => 14.12.2014 08:20:47
[42] => 14.12.2014 09:56:53
[43] => 14.12.2014 11:13:14
[44] => 14.12.2014 13:30:05
)
Answer the question
In order to leave comments, you need to log in
Your array is $dates Will only work if sorted correctly.
$ndates = array();
foreach ( $dates as $key => $date )
{
$ndates[$key] = strtotime($date) - (15*60*60);
}
$filtered_dates = array();
foreach ( $ndates as $key => $timestamp )
{
$day = (int) date('d', $timestamp);
$filtered_dates[$day][] = $key;
}
$grouped = array();
$group = 0;
foreach ($filtered_dates as $groups) {
foreach ($groups as $id){
$grouped[$group][] = $dates[$id];
}
$group++;
}
var_dump($grouped);
Convert everything to timestamp first.
Then walk and scatter everything by day and then by the hour already.
Checking the date odd / even is probably the easiest + if less than 15 hours on the first shift, if more on the second - in the end you get what you should, maybe :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question