Reputation: 77
I'm trying to build a skill tracker that displays how long a user has worked on learning a particular skill. As a result, I have a while loop that loops through everything in my database but I'm stuck on how to get the difference between two times for each iteration through the loop. They are formatted as H:m:s in 12 hour time (e.g. 09:05:00).
For example, let's say I worked on two skills for the date of 7/2/15. The result should look like the following:
| Skill Name | Time Started | Time Finished | Difference |
| ---------- | ------------ | ------------- | ---------- |
| Bootstrap | 9:05:00 AM | 11:15:00 AM | 2:10:00 |
| CSS | 10:50:00 PM | 11:50:00 PM | 1:00:00 |
| Python | 10:00:00 AM | 1:00:00 PM | 3:00:00 |
Here's my code (excuse the deprecated statements. This will only be used by me):
$skilltoview = $_POST['skilltoview'];
include("db.php");
//retrieve data from database
$result = mysql_query("SELECT * FROM timer WHERE skillname='$skilltoview'");
echo "<div class='table-responsive'>";
echo "<table class='table table-striped table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID</th><th>Skill Name</th><th>Skill Date</th><th>Time Started</th><th>Time Ended</th><th>Notes</th><th>Difference</th>";
echo "</tr></thead>";
echo "<tbody>";
function timeDifference($timeEnd, $timeStart){
$tResult = strtotime($timeEnd) - strtotime($timeStart);
return date("G:i", $tResult);
}
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$start = $row['timestarted'];
$stop = $row['timestopped'];
echo "
<tr>
<td>$id</td>
<td>$row[skillname]</td>
<td>$row[date]</td>
<td>$row[timestarted]</td>
<td>$row[timestopped]</td>
<td>$row[notes]</td>
<td>"; print(timeDifference($start, $stop)); echo "</td>
<td><a href ='edittracker.php?id=$id'>Edit</a>
<td><a onClick='deleteConfirm(); return false;' href='#'><center>Delete</center></a></a>
</tr>";
echo "<script type='text/javascript'>
function deleteConfirm(){
var r = confirm('Are you sure you want to delete this skill log?');
if (r == true) {
window.location='deletetracker.php?id=$id';
} else {
x = 'You pressed Cancel!';
}
}
</script>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
//close connection
mysql_close($conn);
Upvotes: 0
Views: 108
Reputation: 5155
Regardless of what's wrong with the php-code, you can make your database-server calculate the difference just as easily; use TIMEDIFF and add that to your query:
SELECT *, TIMEDIFF(timestopped, timestarted) as diff FROM timer WHERE skillname='$skilltoview'
you can then print <td>$row[diff]</td>
directly
Upvotes: 1