-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaceCalculator.php
192 lines (152 loc) · 4.62 KB
/
PaceCalculator.php
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?
if (!$_POST['submit']){
prompt(null, null, false, false);
} else {
$goal = $_POST['goal'];
$distance = $_POST['distance'];
$isKM = $_POST['isKM'] == 1 ? true : false;
$showKM = $_POST['showKM'] == 1 ? true : false;
prompt($goal, $distance, $isKM, $showKM);
calculate($goal, $distance, $isKM, $showKM);
}
function calculate($goal, $distance, $isKM, $showKM) {
if (!validate($goal)) {
echo "Invalid time format. Please enter MM:SS or HH:MM:SS.";
return;
}
$goalInSec = convertToSeconds($goal);
$numSplits = getNumSplits($distance, $isKM, $showKM);
$averagePaceInSec = getAveragePaceInSeconds($goalInSec, $numSplits);
echo "Distance: ";
echo $numSplits . ($showKM ? "km" : " miles");
echo "<br/>";
echo "Goal: " . $goal;
echo "<br/>";
echo "<br/>";
echo "Average pace: " . convertToHHMMSS($averagePaceInSec);
echo "/" . ($showKM ? "km" : "mile");
echo "<br/>";
getSplits($averagePaceInSec, $numSplits);
if (floor($numSplits) != $numSplits) {
echo "Finish: " . $goal;
}
}
function getSplits($averagePaceInSec, $numSplits) {
$clock = 0;
//Starting at 1 instead of 0 because we're displaying mile 1 to start, not mile 0
for ($x = 1; $x <= floor($numSplits); $x++){
$clock += $averagePaceInSec;
$clockInHHMMSS = convertToHHMMSS($clock);
echo $x . ": " . $clockInHHMMSS . "<br/>";
}
}
function convertToSeconds($time) {
$timeSplit = explode(':', $time);
$seconds;
if (count($timeSplit) == 2) {
$seconds = $timeSplit[0]*60;
$seconds += $timeSplit[1];
} else {
$seconds = $timeSplit[0]*60*60;
$seconds += $timeSplit[1]*60;
$seconds += $timeSplit[2];
}
return $seconds;
}
function convertToHHMMSS($timeInSec) {
$hours = floor($timeInSec/3600);
$minutes = floor(($timeInSec-($hours*3600))/60);
$seconds = $timeInSec - (($hours*3600) + ($minutes*60));
$return = "";
if($hours>0){
$return = $hours . ":";
$return .= str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":";
} else {
$return = $minutes . ":";
}
$return .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
return $return;
}
function validate($input) {
$elements = count(explode(':', $input));
if ($elements<2 || $elements>3){
return false;
}
$pattern;
if ($elements == 2){
$pattern = "/^([0-5][0-9]):([0-5][0-9])$/";
} else if ($elements==3){
$pattern = "/^(0[1-9]|1[0-2]):([0-5][0-9]):([0-5][0-9])$/";
}
if (preg_match($pattern, $input)) {
return true;
}
return false;
}
function getNumSplits($distance, $isKM, $showInKM) {
if ($showInKM){
return getNumSplits_KM($distance, $isKM);
} else {
return getNumSplits_Miles($distance, $isKM);
}
}
function getNumSplits_Miles($distance, $isKM){
$const_KMInMile = 1.60934;
$splits;
if ($isKM){
$splits = $distance/$const_KMInMile;
} else {
$splits = $distance;
}
return round($splits, 1, PHP_ROUND_HALF_DOWN);
}
function getNumSplits_KM($distance, $isKM){
$const_KMInMile = 1.60934;
$splits;
if (!$isKM){
$splits = $distance*$const_KMInMile;
} else {
$splits = $distance;
}
return round($splits, 1, PHP_ROUND_HALF_DOWN);
}
function getAveragePaceInSeconds($goal, $numSplits) {
$pace = $goal/$numSplits;
return round($pace, 0, PHP_ROUND_HALF_UP);
}
function prompt($goal, $distance, $isKM, $showKM) {
echo "Enter race distance and goal to get clock time for each mile/km.<br/>";
echo "<form action='" . $_SERVER['PHP_SELF'] . "' method='post' id='form' name='form'>";
echo "Distance: <input type='text' name='distance' size='20' onclick='this.value=\"\";' value='". htmlspecialchars($distance) . "'/>";
echo "<input type='radio' name='isKM' value='0' ";
if (!$isKM){
echo " checked";
}
echo "/>miles ";
echo "<input type='radio' name='isKM' value='1'";
if ($isKM){
echo " checked";
}
echo "/>km";
echo "<br/>Goal time: <input type='text' name='goal' size='20' value='";
if (isset($goal)){
echo $goal;
} else {
echo "HH:MM:SS or MM:SS";
}
echo "' onclick='this.value=\"\";'/>";
echo "<br/>Show results in: ";
echo "<input type='radio' name='showKM' value='0' ";
if (!$showKM){
echo " checked";
}
echo "/>miles ";
echo "<input type='radio' name='showKM' value='1'";
if ($showKM){
echo " checked";
}
echo "/>km";
echo "<br/><input type='submit' name='submit' value='Get splits!'/>";
echo "</form>";
}
?>