I have a database containing events. Every event has a timestamp. The events are evenly spread trough the day, but every night there is a timespan without data. My problem is that the night is not well defined. It can be from 23pm to 7am next day, or from 2am to 10am the same day or even only from 8pm to 23pm the same day.
Now I want to calculate the intervall of the events, but without the big timespan without events. But I have no idea how to find this timespan. My problem is, that there can be days without timespan, or two days with the same timespan (for example 8pm to to midnight of the first day, midnight to 7 am of the second day).
My question is now: How to find this timespan?
I would prefer a solution in MySql only, but if it is not possible it would be possible to use PHP, too.
How about self-joining a table to the following row, then doing a time-diff between the joined tables, and finding the maximum diff?
Assuming your schema is something like this (and assuming entries are in time order):
Something like this: