The logic seems complex and more error-prone. As an improvement, we can share the responsibility between query and server/application itself. For example, consider we need to find events in a particular month of a year say Aug 2020.
From the given timestamp say 1598894650 (in seconds), we can find the number of days elapsed from 1/1/1970 via 1598894650/86400 = 18505. Also, the application can easily find the days range for 1-30 Aug 2020 from 1/1/1970 which will be [18,475, 18506]. Now it just becomes a simple range query. This logic will have better error handling and more predictable results.
In the meantime, I’ll check the current status of this request.