Tag Archives: Code

Freelance “time card” calculator

[ad]I don’t know how many others will find this handy, but I’ve found it handy myself. It’s tools like this that make this site “Random Web Tools.” :)

The script is very simple. I just basically created a format for logging the time you work in a text file, so that you can copy and paste in your “time card” and it will spit out your total time worked. The script could easily be modified to your liking. I threw up an example here. I originally wrote the script and also ported it to PHP.

Enter the times in the following format, separated by new lines or whatever. Whitespace does not matter. The number in the parenthesis is what counts. That’s the total hours:minutes worked during that period of work. The script could be improved by just counting up from the times themselves, but there are lots of problems that could run into unless you included dates, and for personal reasons, I chose not to do that. Don’t forget to change the hourly_rate variable if you want it to tell you how much you get owed.

1:27 PM - 2:08 PM		(0:41)
12:09 AM - 1:12 AM		(1:03)

Demo: http://www.randomtools.net/scripts/times.php

HTML form:

1
2
3
4
<p>Enter the times, separated by new lines or whatever. Whitespace does not matter.</p>
<form method="post" action="">
	<textarea name="times" style="width: 300px; height: 200px;"></textarea><br /><button type="submit">Go</button>
</form>

PHP source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
$hourly_rate = 30; // Change this to your wage
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$arr = array();
	$x = 0;
	$str = $_POST['times'];
	$pos = strpos($str, '(');
	while ($pos > 0) {
		$pos2 = strpos($str, ')');
		$arr[$x] = substr($str, $pos + 1, $pos2 - $pos - 1);
		$str = substr($str, $pos2 + 2, strlen($str));
		$pos = strpos($str, '(');
		$x++;
	}
 
	$hours = 0;
	$minutes = 0;
 
	for ($x = 0; $x < count($arr); $x++) {
		$arr2 = explode(':', $arr[$x]);
		$minutes += $arr2[1];
		if ($minutes >= 60) {
			$hours = $hours + 1;
			$minutes -= 60;
		}
		$hours += $arr2[0];
	}
 
	$total = $hours * 15 + $minutes / 60 * 15;
 
	echo "<p>Worked <b>$hours hours</b> and <b>$minutes minutes</b> (" . number_format($hours + $minutes / 60, 2, '.', '') . ")<br />That's $" . number_format(($hours + $minutes / 60) * $hourly_rate, 2, '.', '') . "</p>";
}
?>

ASP source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%
hourly_rate = 30 'Change this to your wage
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
	ReDim arr(0)
	x = 0
	str = Request("times")
	pos = InStr(str, "(")
	While pos > 0
		ReDim Preserve arr(x)
		pos2 = InStr(str, ")")
		arr(x) = Mid(str, pos + 1, pos2 - pos - 1)
		str = Mid(str, pos2 + 2, Len(str))
		pos = InStr(str, "(")
		x = x + 1
	Wend
 
	hours = 0
	minutes = 0
 
	For x = 0 To UBound(arr)
		arr2 = Split(arr(x), ":")
 
		minutes = minutes + CInt(arr2(1))
 
		If minutes >= 60 Then
			hours = hours + 1
			minutes = minutes - 60
		End If
 
		hours = hours + CInt(arr2(0))
	Next
 
	total = hours * 15 + minutes / 60 * 15
 
	Response.Write("<p>Worked <b>" & hours & " hours</b> and <b>" & minutes & " minutes</b> (" & FormatNumber(hours + minutes / 60, 2) & ")<br />That's " & FormatCurrency((hours + minutes / 60), 2) & "</p>")
End If
%>