Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day,

I was wondering how to solve this dilemma of mine. I am not sure if getting time difference would be the best approach.

Objective: Get actual holiday working hours during holidays.

Situation: Every 25th of December here in PH is a legal holiday and assuming our employee starts working on 24th of December at 08:00 PM to 25th of December 4:30 AM, we would like to get the exact number of hours how long an employee worked during the holiday. Holiday = 'December 25, 2013'

For instance please see table details below.


HTML
Emp_name         Time_in                 Time_Out

Joey             12/24/2013 20:00:00     12/25/2013 04:30:00




Expected results: 4 hours & 30 minutes

Thanks.
Posted
Updated 15-Apr-13 22:48pm
v9
Comments
[no name] 15-Apr-13 13:52pm    
Isn't the expected result 6 hours and 30 mins according to the table ?
Richard C Bishop 15-Apr-13 13:57pm    
It is indeed.
Maciej Los 15-Apr-13 14:17pm    
Yes, but OP want to count only working hours in holiday. So, it's 4:30 ;)
Richard C Bishop 15-Apr-13 15:06pm    
Good catch.
Richard C Bishop 15-Apr-13 13:58pm    
As learner'sbug stated above, the difference in time is 6 hours and 30 minutes. I am not very familiar with MySQL but if it has a function like DATEDIFF that MSSQL has, then that is what you want.

I don't have MySQL but here is what it looks like in Microsoft SQL Server Transact-SQL. This example handles the situation where there are no hours during the holiday and returns 00:00:00 for that case.

SQL
-- Create temp table for demonstration
Declare  @tb table (emp_name varchar(30),time_in DateTime, time_out DateTime);
insert into @tb (emp_name,time_in,time_out) values( 'Joey','2013-12-24 20:00:00','2013-12-25 04:30:00');
insert into @tb (emp_name,time_in,time_out) values( 'Not Joey','2013-12-24 20:00:00','2013-12-24 22:30:00');
insert into @tb (emp_name,time_in,time_out) values( 'Still Not Joey','2013-12-26 20:00:00','2013-12-26 22:30:00');
Declare @Holiday DateTime;
Set @Holiday = '2013-12-25 00:00:00'
--
-- Execute the SELECT statement
--
select
 emp_name,CAST(time_in as smalldatetime) As TimeIn,CAST(time_out as smalldatetime) as TimeOut,
 DATEADD(
    second,
    DATEDIFF(second,time_in,time_out),
    TIMEFROMPARTS(0,0,0,0,0)) as Total_Hours,
 DATEADD(second,
   IIF( DATEDIFF(second,
        IIF(@Holiday > time_in,@Holiday,time_in),
        IIF(DATEADD(Hour,24,@Holiday)< time_out,DATEADD(Hour,24,@Holiday),time_out))<0,0,
    DATEDIFF(second,
        IIF(@Holiday > time_in,@Holiday,time_in),
        IIF(DATEADD(Hour,24,@Holiday)< time_out,DATEADD(Hour,24,@Holiday),time_out))),TIMEFROMPARTS(0,0,0,0,0)) as Holiday_Hours
 from @tb


CSS
emp_name        TimeIn              TimeOut             Total_Hours Holiday_Hours
Joey            2013-12-24 20:00:00 2013-12-25 04:30:00   08:30:00    04:30:00
Not Joey        2013-12-24 20:00:00 2013-12-24 22:30:00   02:30:00    00:00:00
Still Not Joey  2013-12-26 20:00:00 2013-12-26 22:30:00   02:30:00    00:00:00



Attempt at MySQL version (without benefit of MySQL database software):
SQL
select
 emp_name,time_in,time_out,
 DATE_ADD(TIMEFROMPARTS(0,0,0,0,0), INTERVAL TIME_TO_SEC(TIMEDIFF(time_out,time_in)) SECOND) as Total_Hours,
 DATE_ADD(TIMEFROMPARTS(0,0,0,0,0), INTERVAL
    IIF(
      TIME_TO_SEC(TIMEDIFF(
    IIF(
         DATE_ADD('2013-12-25 00:00:00',INTERVAL 24 HOUR) < time_out,
     DATE_ADD('2013-12-25 00:00:00',INTERVAL 24 HOUR),time_out
           ),
         IIF('2013-12-25 00:00:00' > time_in,'2013-12-25 00:00:00',time_in)
                )
    )<0,0,
    TIME_TO_SEC(
    TIMEDIFF(
        IIF(DATE_ADD('2013-12-25 00:00:00',INTERVAL 24 HOUR) < time_out,DATE_ADD('2013-12-25 00:00:00',INTERVAL 24 HOUR),time_out)
        ),
            IIF('2013-12-25 00:00:00' > time_in,'2013-12-25 00:00:00',time_in)
        )
    ) SECOND) as Holiday_Hours
 from @tb
 
Share this answer
 
v7
Comments
JMAM 16-Apr-13 8:01am    
Does anyone here knows how to transfer above query from SQL to MySQL version?

i am having an error when using it to mysql it has error like below:


[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CAST(time_in as smalldatetime) As TimeIn,CAST(time_out as smalldatetime) as Time' at line 2
JMAM 16-Apr-13 16:29pm    
I found out at that MS SQL & MySQL has some major difference on their syntax that might be a problem for me.

DATE_ADD(date,INTERVAL expr type) - MySQL
DATEADD(datepart,number,date) - MS SQL


DATEDIFF(date1,date2) - MySQL
DATEDIFF(datepart,startdate,enddate) - MS SQL

That is why i am having those errors, your code is simple and brief that's why i like it, but i am afraid i would be needing to move to MS SQL just for this special function.
Mike Meinz 16-Apr-13 16:37pm    
Can't you make the changes to the DATEADD and DATEDIFF function parameters so that the query will work on MySQL? It looks like you just rearrange the parameters for DATEADD to MySQL's DATE_ADD and remove one parameter for MS SQL DATEDIFF to MySQL's DATEDIFF.
Mike Meinz 17-Apr-13 8:26am    
See MySQL addition to Solution 1. I found some MySQL documentation via Google search and some examples. I attempted to convert the MS SQL SELECT statement to a MySQL SELECT statement. I did a bunch of new lines and indentations to try to match up the parenthesis correctly. I have no MySQL database to test on so there may be ned of adding parenthesis or other adjustments.
JMAM 17-Apr-13 9:11am    
Thank you so much sir, i am working with it now, I will update you as soon as it works with me. Now its quiet challenging at my side to solve this simple query, I have to read more and more and make a lot of experience. Honestly i am quite worried because i am running out of time and i need to finish this first edition of the software project.


I found out that TIMEFROMPARTS function doesn't work at MySQL too. Sorry for the trouble i am causing you. i know MySQL is free to download but if you need MySQL installer i can upload it my dropbox account.

I really need to translate and use this perfect code because i will use it in my app several times.

Here you will find MySQL essentials, Gui and Navicat for MySQL at: https://dl.dropboxusercontent.com/u/65392916/Database%20Tools.rar
Please, follow below links:
DATE_SUB function.php[^]
TIMEDIFF function[^]

Steps to do:
1) Substract hours from TimeOut to get zero time (12/25/2013 00:00:00)
2) get time diff between substracted date (vide: 1) and TimeOut

That's all!
 
Share this answer
 
Comments
JMAM 15-Apr-13 22:51pm    
Thanks, Maciej Los, Actually i have tried this.

Yes this will work if, Holiday is Dec 25, 2013 and worker starts at December 24, 2013 - 10:00 PM to December 25, 2013 4:30 AM
The result below will return a value of counted minutes that is equivalent to (270) which i can devided to (60) to get the result of 4.5 working hours

Dim dt1 As DateTime = 12/25/2013 04:30:00
Dim dt2 As DateTime = 12/25/2013 00:00:00)
Dim ts As TimeSpan = dt2.Subtract(dt1)
MessageBox.Show(Convert.ToInt32(ts.TotalMinutes) & " minutes time Late")

Actual Message = "270 minutes time Late".


Possible problems on different situations:

Situation # 1

If an employee started to work December 25, 2013 3:00 AM to December 25, 2013 11:00 PM.
when "12/25/2013 00:00:00" is subtracted by "12/25/2013 23:00:00"
System will provide results of (660) minutes, when the expected answer is only (480) if i'am not mistaken, this can be handled by getting the time diiference on a time in then subtract it to the result.







Situation # 2



If an employee started to work on December 25, 2013 10PM to December 26, 2013 4:30 AM. expected answer is (120)

with the following code above, System will show answer like "-390" which is not correct



Iam still thinking about other situations that may bring discrepancies here.
Maciej Los 16-Apr-13 2:02am    
If you want to do that on server side (MySQL), you need to define table with holidays and its hours. Then, you need to compare working time with holiday's hours.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900