I'm developing an attendance application using PHP with PostgreSQL as the database.
Here is my table:
- ID - int, PK
- Name - character varying(50)
- In - time without time zone
- Out - time without time zone
Basically, I want to check how many person are there in a specified time. For example, if the input is 08:00, then all person signed in before 08:00 and signed out after 08:00 will be counted.
The problem I encountered is when Out < In, which means the person signed in before 00:00 and signed out after 00:00. I tried to solve this problem by using the following SQL:
SELECT COUNT(*) FROM(
SELECT tbl_1."ID" FROM attendance as tbl_1 WHERE "In" < "Out" AND "In" < '$input' AND "Out" > '$input'
UNION
SELECT tbl_2."ID" FROM attendance as tbl_2 WHERE "Out" < "In" AND "In" < '$input2' AND "Out" > '$input'
) AS tmp_tbl
$input is a PHP variable storing the input data (string). $input2 is a PHP variable storing the input data +24 hrs (I replaced the first two characters).
But unfortunately I encountered another problem - that PostgreSQL is unable to compare time with value >24:00.
Query failed: ERROR: date/time field value out of range: "35:11:00"
Can anyone help me out with this problem, other than adding another columns for DateIn and DateOut? storing information about the date?
Sorry for my english.