Я получаю время начала и окончания из моих событий календаря Google в виде массива:
Array
(
[0] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-21T19:30:00+02:00
[start] => 2019-10-21T18:30:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[1] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-22T15:00:00+02:00
[start] => 2019-10-22T14:00:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[2] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-23T16:30:00+02:00
[start] => 2019-10-23T15:30:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[3] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-23T18:00:00+02:00
[start] => 2019-10-23T17:00:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[4] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-24T16:30:00+02:00
[start] => 2019-10-24T15:30:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[5] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-25T17:30:00+02:00
[start] => 2019-10-25T16:30:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[6] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-28T13:30:00+01:00
[start] => 2019-10-28T12:30:00+01:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
)
Теперь я хотел бы удалить все элементы этого массива, где: - время начала и окончания предшествует 16 o'часы ИЛИ - время начала после 18 часов
Для этого я использую следующий код:
$TerminStart = new DateTimeImmutable($events[$i]['start']);
$TerminEnde = new DateTimeImmutable($$events[$i]['end']);
if ($TerminStart->setTime(16, 0)->diff($TerminStart)->invert == 1 && $TerminEnde->setTime(16, 0)->diff($TerminEnde)->invert == 1) {
unset($busy_obj[$i]);
} elseif ($TerminStart->diff($TerminStart->setTime(18, 0))->invert == 1) {
unset($busy_obj[$i]);
}
Мой массив событий выглядит так:
Array
(
[2] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-23T16:30:00+02:00
[start] => 2019-10-23T15:30:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[3] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-23T18:00:00+02:00
[start] => 2019-10-23T17:00:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[4] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-24T16:30:00+02:00
[start] => 2019-10-24T15:30:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[5] => Google_Service_Calendar_TimePeriod Object
(
[end] => 2019-10-25T17:30:00+02:00
[start] => 2019-10-25T16:30:00+02:00
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
)
Пока все хорошо (я надеюсь, что это лучшая практика. Если нет, пожалуйста, поддержите меня :))
Теперь проблема: с новой версией моего массива я знаю, какие события происходят между 16 часами. и 18 часов. Но теперь я должен знать, если у меня разница в 1 час до или после события между 16 и 18 часами.
Это будет решение в качестве примера:
[2] [start] => 2019-10-23T15:30:00+02:00 || [end] => 2019-10-23T16:30:00+02:00
[3] [start] => 2019-10-23T17:00:00+02:00 || [end] => 2019-10-23T18:00:00+02:00
- >This events are on the same day.
--> Event [2] have no 1h difference between start time an 16 o'clock
--> Event [2] have no 1h difference between end time and start time of event [3]
--> Event [3] have no 1h difference between start time and end time of event [2]
--> Event [3] have no 1h difference between end time an 18 o'clock
[4] [start] => 2019-10-24T15:30:00+02:00 || [end] => 2019-10-24T16:30:00+02:00
--> Event [4] have no 1h difference between start time an 16 o'clock
--> Event [4] have 1h difference between end time an 18 o'clock
[5] [start] => 2019-10-25T16:30:00+02:00 || [end] => 2019-10-25T17:30:00+02:00
--> Event [5] have no 1h difference between start time an 16 o'clock
--> Event [5] have no 1h difference between end time an 18 o'clock
Iнадеюсь, что вы можете поддержать меня в этой ситуации. Большое спасибо !!