# Time Sheet Help

I am trying to develop a time sheet app. I am having a lot of trouble with getting the time function to work. Can anyone help?
``````<cfscript>
function test(h1, h2, h3, h4, m1, m2, m3, m4, a, b, c, d){

hour1  = h1;
hour2  = h2;
hour3  = h3;
hour4  = h4;
min1   = m1;
min2   = m2;
min3   = m3;
min4   = m4;
Merid1 = a;
Merid2 = b;
Merid3 = c;
Merid4 = d;
Total  = 0;

//FIRST TIMESET
//starting hour

if(hour1 eq 0){
hour1 = 0;

}
else{
if(hour1 lt 12){hour1;}
if(hour1 eq 12 and Merid1 eq "AM"){hour1 = hour1 - 12;}
if(hour1 eq 12 and Merid1 eq "PM"){hour1;}
if(hour1 lt 12 and hour1 gt 0 and Merid1 eq "PM"){hour1 = hour1 + 12;}
if(hour1 eq 0)(hour1);
}

//ending hour
if(hour2 eq 0){
hour2 = 0;

}
else{
if(hour2 lt 12){hour2;}
if(hour2 eq 0  and Merid2 eq "AM"){hour2;}
if(hour2 lt 12 and hour2 gt 0 and Merid2 eq "AM"){hour2 = hour2 + 24;}
if(hour2 eq 12 and Merid2 eq "AM"){hour2 = hour2 - 12;}
if(hour2 eq 12 and Merid2 eq "PM"){hour2;}
if(hour2 lt 12 and Merid2 eq "PM"){hour2 = hour2 + 12;}
}

if(hour3 eq 0){
hour3 = 0;
}

else{
//SECOND TIMESET
//starting hour
if(hour3 lt 12){hour3;}
if(hour3 eq 12 and Merid3 eq "AM"){hour3 = hour3 - 12;}
if(hour3 eq 12 and Merid3 eq "PM"){hour3;}
if(hour3 lt 12 and hour3 gt 0 and Merid3 eq "PM"){hour3 = hour3 + 12;}
if(hour3 eq 0)(hour3);
}

if(hour4 eq 0){
hour4 = 0;
}
else{
//ending hour
if(hour4 lt 12){hour4;}
if(hour4 eq 0  and Merid4 eq "AM"){hour4;}
if(hour4 lt 12 and hour4 gt 0 and Merid4 eq "AM"){hour4 = hour4 + 24;}
if(hour4 eq 12 and Merid4 eq "AM"){hour4 = hour4 - 12;}
if(hour4 eq 12 and Merid4 eq "PM"){hour4;}
if(hour4 lt 12 and Merid4 eq "PM"){hour4 = hour4 + 12;}

}

TempMinTotal1 =  min2 - min1;
if(TempMinTotal1 lt 0){
TempMinTotal1 = TempMinTotal1 + 60;}
if(TempMinTotal1 eq "0"){
TempMinTotal1 = TempMinTotal1 & "0";

}

TempMinTotal2 =  min4 - min3;
if(TempMinTotal2 lt 0){
TempMinTotal2 = TempMinTotal2 + 60;}
if(TempMinTotal2 eq "0"){
TempMinTotal2 = TempMinTotal2 & "0";

}

if(TempminTotal2 eq 0){
MinTotal = TempMinTotal1;

}
else{
MinTotal = TempminTotal2 - TempMinTotal1;
}

TempTotal1  = val(hour2) - val(hour1);
TempTotal2  = val(hour4) - val(hour3);

if (TempTotal1 lt 0){
}
if (TempTotal2 lt 0){
}

/*if(min4 lt min3){
TempTotal2 = TempTotal2 - 1;
}

if(min2 lt min1){
TempTotal1 = TempTotal1 - 1;

}*/

Total = TempTotal1 + TempTotal2;

if(Total gt 23){
}
if (listLen(request.errorlist,"-")){
return request.errorlist;
}
Time = Total & " hrs " & MinTotal & " min ";
return Time;

}

</cfscript>
``````
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
Here is the form page
``````<cfinclude template="resources/includes/top.cfm">
<cfinclude template="resources/functions/TimeCalc.cfm">
<cfparam name="URL.a"            default="">
<cfparam name="form.a" 			 default="#url.a#">
<cfparam name="form.date"        default="">
<cfparam name="form.in"			 default="">
<cfparam name="form.out" 		 default="">
<cfparam name="form.in2" 		 default="">
<cfparam name="form.out2" 		 default="">
<cfparam name="form.min1"        default="">
<cfparam name="form.Meridiem1" 	 default="">
<cfparam name="form.Meridiem2" 	 default="">
<cfparam name="form.Meridiem3" 	 default="">
<cfparam name="form.Meridiem4" 	 default="">
<cfscript>
request.errorList = "";

</cfscript>
<cfset todayDate = Now()>
<cfset Date = #DaysInMonth(todayDate)#>
<Cfset months = #MonthAsString(month(now()))#>
<cfset years = #year(now())#>
<cfset TimeTotal = "">

<cfoutput>
<cfswitch expression="#form.a#">
<cfcase value="1">
<cfscript>
if(not len(trim(form.in))) request.errorList = listAppend(request.errorList,"Please enter a Start Time","|");
if(not len(trim(form.out))) request.errorList = listAppend(request.errorList,"Please enter a End Time","|");
//if(not len(trim(form.in2))) request.errorList = listAppend(request.errorList,"Please enter a Start Time","|");
//if(not len(trim(form.out2))) request.errorList = listAppend(request.errorList,"Please enter a End Time","|");
if(form.date eq "date") request.errorList = listAppend(request.errorList, "You must select a date", "|");
if(form.in does not contain ":") request.errorList = listAppend(request.errorList, "Time has to be in this format 00:00 AM/PM", "|");

form.min1   	= val(listrest(form.in,  ":"));
form.Timein  	= val(listfirst(form.in, ":"));
form.min2   	= val(listrest(form.out,  ":"));
form.Timeout  	= val(listfirst(form.out, ":"));
form.min3   	= val(listrest(form.in2,  ":"));
form.Timein2  	= val(listfirst(form.in2, ":"));
form.min4   	= val(listrest(form.out2,  ":"));
form.Timeout2  	= val(listfirst(form.out2, ":"));

if(min1 gt 59 or form.min2 gt 59 or form.min3 gt 59 or form.min4 gt 59) request.errorList = listAppend(request.errorList, "Your minutes part less that 60 minutes", "|");
if(form.Timein gt 12) request.errorList = listAppend(request.errorList,"Start Time must be less than or equal to 12:00 pm/am","|");
if(form.Timeout gt 12) request.errorList = listAppend(request.errorList,"End Time must be less than or equal to 12:00 pm/am","|");
if(form.Timein2 gt 12) request.errorList = listAppend(request.errorList,"Start Time must be less than or equal to 12:00 pm/am","|");
if(form.Timeout2 gt 12) request.errorList = listAppend(request.errorList,"Start Time must be less than or equal to 12:00 pm/am","|");
</cfscript>
<Cfset TimeTotal = #test(Timein, Timeout, Timein2, Timeout2, min1, min2, min3, min4, Meridiem1, Meridiem2, Meridiem3, Meridiem4)#>
<cfscript>
if(TimeTotal  eq "false") request.ErrorList = listAppend(request.errorList,"Time has to be within a 24 hour period","|");
</cfscript>
<cfif request.errorList eq "">
#TimeTotal#
</cfif>
</cfcase>
<cfcase value="2">
<cfscript>
form.min1   	= "";
form.Timein  	= "";
form.min2   	= "";
form.Timeout  	= "";
form.min3   	= "";
form.Timein2  	= "";
form.min4   	= "";
form.Timeout2  	= "";
form.Meriediem1 = "";
form.Meriediem2 = "";
form.Meriediem3 = "";
form.Meriediem4 = "";
</cfscript>
</cfcase>
</cfswitch>
</cfoutput>

<div id="content">

<center>
<cfoutput>
<h1>Testing Place</h1>
<form action="#cgi.SCRIPT_NAME#" method="post" name="form">
<input type="hidden" name="a" value="1" />
<table border="1">
<cfif listLen(request.errorList,"|")>
<tr align="center">
<td colspan="9" style="color:##FF0000; font-weight:bold;">
<u>ERROR</u>:
<ul>
<cfloop index="i" list="#request.errorList#" delimiters="|">
<li>#i#</li>
</cfloop>
</ul>
</td>
</tr>
</cfif>
<tr bgcolor="##00CCFF">
<td>Date</td>
<td>Start Time 1</td>
<td>AM or PM</td>
<td>End Time 1</td>
<td>AM or PM</td>
<td>Start Time 2</td>
<td>AM or PM</td>
<td>End Time 2</td>
<td>AM or PM</td>
</tr>
<tr>
<td width="100px">
<select name="date" style="width:150px;">
<Cfif form.a eq 1>
<option value="#form.date#">#form.date#</option>
<cfelse>
<option value="date">Date</option>
</Cfif>
<cfloop index="i" from="1" to="#Date#">
<option>#months# #i# #years#</option>
</cfloop>
</select>

</td>
<td>
<input type="text" name="in" value="#form.in#" />
</td>
<td>
<select name="Meridiem1">
<cfif form.a eq 1>
<option value="#form.Meridiem1#">#form.Meridiem1#</option>
</cfif>
<cfif form.Meridiem1 eq "AM"><cfelse><option value="AM">AM</option></cfif>
<cfif form.Meridiem1 eq "PM"><cfelse><option value="PM">PM</option></cfif>
</select>
</td>

<td>
<input type="text" name="out" value="#form.out#" />
</td>
<td>
<select name="Meridiem2">
<cfif form.a eq 1>
<option value="#form.Meridiem2#">#form.Meridiem2#</option>
</cfif>>
<cfif form.Meridiem2 eq "AM"><cfelse><option value="AM">AM</option></cfif>
<cfif form.Meridiem2 eq "PM"><cfelse><option value="PM">PM</option></cfif>
</select>
</td>

<td>
<input type="text" 	name="in2" value="#form.in2#" />
</td>
<td>
<select name="Meridiem3">
<cfif form.a eq 1>
<option value="#form.Meridiem3#">#form.Meridiem3#</option>
</cfif>
<cfif form.Meridiem3 eq "AM"><cfelse><option value="AM">AM</option></cfif>
<cfif form.Meridiem3 eq "PM"><cfelse><option value="PM">PM</option></cfif>

</select>
</td>

<td>
<input type="text" name="out2" value="#form.out2#" />
</td>
<td colspan="2">	<select name="Meridiem4">
<cfif form.a eq 1>
<option value="#form.Meridiem4#">#form.Meridiem4#</option>
</cfif>
<cfif form.Meridiem4 eq "AM"><cfelse><option value="AM">AM</option></cfif>
<cfif form.Meridiem4 eq "PM"><cfelse><option value="PM">PM</option></cfif>
</select>
</td>

</tr>
</table>
<br />
<br />
<cfif val(TimeTotal) gt 0><p>Your Total Time for <strong>#form.date#</strong> is: <h1>#TimeTotal#</h1></cfif>
<br />
<br />
<input type="image" src="assets/images/submit.png"/><a href="#cgi.SCRIPT_NAME#?a=2"><img src="assets/images/reset.png" height="24" width="132" border="0" /></a>
</form>
</cfoutput>
</center>

</div>
</body>
</html>
``````
Commented:
I think you are over complicating things. Just convert your form values into datetime objects. Then you can use dateDiff to calculate the total number of minutes worked.  Once you have that information it is a simple matter to calculate hours and minutes.

* Hours -> Divide the total minutes by 60 to get the number of hours
* Minutes -> Use MOD 60 to get the remaining minutes

``````<!--- needs validation --->
<cfset form.in = "8:00">
<cfset form.Meridiem1= "AM">
<cfset form.out = "12:00">
<cfset form.Meridiem2= "PM">
<cfset form.in2 = "1:00">
<cfset form.Meridiem3= "PM">
<cfset form.out2 = "5:30">
<cfset form.Meridiem4= "PM">

<cfset firstStartTime = parseDateTime(form.in &" "& form.Meridiem1)>
<cfset firstEndTime = parseDateTime(form.out &" "& form.Meridiem2)>
<cfset firstMinutes = dateDiff("n", firstStartTime, firstEndTime)>

<cfset secondStartTime = parseDateTime(form.in2 &" "& form.Meridiem3)>
<cfset secondEndTime = parseDateTime(form.out2 &" "& form.Meridiem4)>
<cfset secondMinutes = dateDiff("n", secondStartTime, secondEndTime)>

<cfset totalMinutes = firstMinutes + secondMinutes>
<cfoutput>
#int(totalMinutes / 60)# hours and #(totalMinutes mod 60)# minutes
</cfoutput>
``````

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
If the response solved your problem, do not forget to close this question.
Commented:
I've requested that this question be deleted for the following reason:

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Not enough information to confirm an answer.

If you feel this question should be closed differently, post an objection and a moderator will read all objections and then close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Auto Deleter
Commented:
I don't think this should be deleted.  The original question was how to calculate the total hours and minutes worked, which is demonstrated by the example in this answer.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.