# Time sheet function help?

Page 1 of 1

## 3 Replies - 2244 Views - Last Post: 18 November 2008 - 05:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=72493&amp;s=b82df96aa45e76ec9282fc482a4eba75&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jgonzalez14

Reputation: 0
• Posts: 8
• Joined: 18-November 08

# Time sheet function help?

Posted 18 November 2008 - 03:45 PM

Hi, I am trying to develop a simple time sheet calculator. This app will take in a day's work time and tell you how many hours you worked. I am have an extremely hard time getting the minutes part to match up. The hours is fine but the minutes does not want to add right. There has got to be an easier way for this to work. I have attached my function and index.
```<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;}
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;}
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";

}

vMinTotal1 = min1 + min2;
vMinTotal2 = min3 + min4;

if(TempminTotal2 eq 0){
MinTotal = TempMinTotal1;

}
if(TempMinTotal1 gt TempminTotal2){
MinTotal = TempminTotal1 - TempMinTotal2;

}
else{
MinTotal = TempminTotal2 - TempMinTotal1;
}

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

Total = TempTotal1 + TempTotal2;

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

}

</cfscript>

```

Index

```<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>

```

Is This A Good Question/Topic? 0

## Replies To: Time sheet function help?

### #2 sansclue

• D.I.C Regular

Reputation: 29
• Posts: 316
• Joined: 21-November 07

## Re: Time sheet function help?

Posted 18 November 2008 - 04:22 PM

jgonzalez14, on 18 Nov, 2008 - 02:45 PM, said:

I am have an extremely hard time getting the minutes part to match up. The hours is fine but the minutes does not want to add right. There has got to be an easier way for this to work.

It is a lot simpler if you take advantage of the built in date and time functions. First, convert your time values into date time objects :

<!--- skipping validation to keep things brief --->
<cfset startTime1 = ParseDateTime("08:00 AM")>
<cfset endTime1 = ParseDateTime("11:30 AM")>
<cfset startTime2 = ParseDateTime("12:00 PM")>
<cfset endTime2 = ParseDateTime("4:30 PM")>

Then use date functions to calculate the total number of minutes worked:

<cfset min1 = dateDiff("n", startTime1, endTime1)>
<cfset min2 = dateDiff("n", startTime2, endTime2)>
<cfset totalMin = min1 + min2>

Finally, use some simple math to calculate the number of hours and minutes.

<cfset hoursWorked = int(totalMin / 60)>
<cfset minWorked = totalMin mod 60>
<cfoutput>
#hoursWorked# hrs #minWorked# min
</cfoutput>

### #3 jgonzalez14

Reputation: 0
• Posts: 8
• Joined: 18-November 08

## Re: Time sheet function help?

Posted 18 November 2008 - 04:32 PM

sansclue, on 18 Nov, 2008 - 03:22 PM, said:

jgonzalez14, on 18 Nov, 2008 - 02:45 PM, said:

I am have an extremely hard time getting the minutes part to match up. The hours is fine but the minutes does not want to add right. There has got to be an easier way for this to work.

It is a lot simpler if you take advantage of the built in date and time functions. First, convert your time values into date time objects :

<!--- skipping validation to keep things brief --->
<cfset startTime1 = ParseDateTime("08:00 AM")>
<cfset endTime1 = ParseDateTime("11:30 AM")>
<cfset startTime2 = ParseDateTime("12:00 PM")>
<cfset endTime2 = ParseDateTime("4:30 PM")>

Then use date functions to calculate the total number of minutes worked:

<cfset min1 = dateDiff("n", startTime1, endTime1)>
<cfset min2 = dateDiff("n", startTime2, endTime2)>
<cfset totalMin = min1 + min2>

Finally, use some simple math to calculate the number of hours and minutes.

<cfset hoursWorked = int(totalMin / 60)>
<cfset minWorked = totalMin mod 60>
<cfoutput>
#hoursWorked# hrs #minWorked# min
</cfoutput>

Are you serious!? ugh Thank you man I was pulling my hair out lol

### #4 sansclue

• D.I.C Regular

Reputation: 29
• Posts: 316
• Joined: 21-November 07

## Re: Time sheet function help?

Posted 18 November 2008 - 05:24 PM

jgonzalez14, on 18 Nov, 2008 - 03:32 PM, said:

Are you serious!? ugh Thank you man I was pulling my hair out lol

Yeah, I know what you mean. We've all been through it :-)