When i add events that are only like 2 hours long e.g.
start 2023-12-24 14:00
end 2023-12-24 16:00
They are not shown when i use the script after 16:00
The issue lies in the comparison of timestamps within the findEvents function. The comparison of timestamps for events uses both date and time, causing events that occur on the same day but at different times to be improperly filtered out.
To rectify this, I modified the function to focus solely on the date part of the timestamps for comparison, disregarding the time component. This was achieved by extracting and comparing only the dates (in the format 'Y-m-d').
Here's the problem demonstrated:
`
private function findEvents(DateTime $date)
{
$found_events = array();
if (isset($this->events)) {
foreach ($this->events as $event) {
// Comparison that considers both date and time
if ($date->getTimestamp() >= $event->start->getTimestamp() && $date->getTimestamp() <= $event->end->getTimestamp()) {
$found_events[] = $event;
}
}
}
return $found_events ?: false;
}
`
and here is the corrected version
`private function findEvents(DateTime $date)
{
$found_events = array();
if (isset($this->events)) {
foreach ($this->events as $event) {
// Extracting and comparing only the dates (Y-m-d) to avoid time-based exclusion
$eventStartDate = (new DateTime($event->start->format('Y-m-d')))->getTimestamp();
$eventEndDate = (new DateTime($event->end->format('Y-m-d')))->getTimestamp();
$inputDate = (new DateTime($date->format('Y-m-d')))->getTimestamp();
if ($inputDate >= $eventStartDate && $inputDate <= $eventEndDate) {
$found_events[] = $event;
}
}
}
return $found_events ?: false;
}
`
This modification ensures that the comparison focuses solely on the dates of events and input, disregarding the time part. This way, events occurring on the same day will be appropriately included irrespective of the specific times they occur.