Skip to content
Advertisement

Find records between two time slots in php

i have time slots like

JavaScript

I have records with updated time as 23:15:00, 23:30:00, 00:15:00, 09:15:00 etc.

What i’m trying to find is the sum of records between each of the $timeslot. I’m not considering what day got updated, only time i’m looking for.

i tried with:-

JavaScript

The expected output is:-

JavaScript

Advertisement

Answer

Strtotime is not required since your time can be compared as strings.

This code works as you expected.

JavaScript

The if with ‘substr’ is needed because for midnight you have ’00’ and not ’24’ so the computer thinks is an empty set (such as hours bigger then 23 and smaller then 0).

Comparison is made between string because bigger time is also a bigger string since you use 2 digits for hours.

You need to count equal slots so you need an array with an element for each slot and increment if duplicate or create an element if not found (the condition ‘!isset’).

Update for modification request

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement