May
31
2018

After looking for a while, I was unable to find a simple web based (read PHP) app that would allow me to just simply track my time on projects using only a few simple criteria.  I opted to do what any self respecting old school PHP scripter would do; build my own.  I decided to use timestamps for the start and stop times, which then presents that whole dealing with date formatting and addition thing.

To be clear, there are a great multitude of project apps out there.  Do a quick search in my favorite privacy honoring browser DuckDuckGo or in SourceForge.  Tons of them.  Yet when you drill down into the details many if not most of them are really complex giving you options for numerous employees, reports, and countless amounts of really nice features I didn’t want to need.  Typically many features translates into much heavy coding along with an oft times significant learning curve or set up time to get to the point where you can begin using them.  Which was beginning to look like weeks worth of creating/deleting databases, inserting info to fill said databases, all so I can simply try it out and see if it will work.  In most cases there weren’t screen shots available to clearly tell me if it was something I could use for my purposes.  So if you’re looking and don’t feel like going the code-your-own route, do a search.  You won’t be disappointed with the choices available.

Now on to my thing.  Within a few hours I had something I could start using immediately to begin tracking my time.  I didn’t yet care about viewing how much time was stacking up, but just wanted to start timing right now.  Using a browser on my computer or phone, wherever I happened to be.  As time has allowed I’ve spent a few hours here and there adding some features, but still have a working timer serving my needs with less than a day of coding involved.

This morning I decided to add a function to peek at the time that’s accumulated in a manner we humans like, you know, time and date formatting rather than just looking at a start and end timestamp, which was how I opted to record time with this little app.  I first turned to the web, where these days you can pretty much find any coding example to do about anything.  Some times your searching returns partial results but not quite what you’re looking for which is when you fill in the gap yourself.  This was one of those situations.  It’s quite possible someone has put all of this together but it wasn’t coming up.  So after putting the bits together I thought this would make a nice post for the tech blog.

I wanted to convert these start and stop timestamps to human readable format AND to calculate the time spent for each record AND calculate the total time on the project.  Simple concept but I wasn’t finding the answer.  I’m not going to get into what a timestamp is, it’s already too well documented all over the web.

For a little review, this is pretty well documented, this is how you you do a simple conversion from a timestamp to a readable date/time format:

<?php
echo date("F j, Y, h:i:s a",$timestamp);
?>

The above gives you this: May 30, 2018, 07:22:07 am

Take a look at the PHP date() function reference for more info on how this works and how you can alter the format.

That brief code is great for making sense of that timestamp, yet not quite as helpful when it comes to calculating the difference between the start and stop times.  One result in my search on this returned a stackoverflow.com answer.  The top answer is a really nice solution which utilizes the OOP DateTime function in PHP.  I like this and will use it along with the PHP date() function mentioned earlier to return the start and end times and difference between the two for each record:

<?php
$start = new DateTime(date("F j, Y, h:i:s a",$clockin));
$end = new DateTime(date("F j, Y, h:i:s a",$clockout));
$interval = $start->diff($end);
echo $interval->format('%d days %H hours %i minutes %s seconds');
?>

Nice yeah?  But how about calculating the total time spent for all records?  Not to mention I really need hours, minutes and seconds for a billable number.  For that we’ll use plain old math along with the floor() function which simply rounds a number down to a whole number.

To simply get the total number of seconds between the start and stop timestamps for each record you simply subtract the start from the stop time:

$TotalTime = $clockout - $clockin;

I’m using a while() loop to iterate through each database record to parse the data and within that loop I’m adding the difference in seconds to a total like this:

$TotalTime = $TotalTime + ($clockout - $clockin);

After the loop finishes I have the total number of seconds elapsed for each record all totaled up and held in the variable $totaltime.  Now it’s time to convert the seconds into hours, minutes and seconds.

First let’s calculate the hours.  You see why we start with hours soon.  There are 60 seconds in each minute and 60 minutes in each hour.  The calculation for this is:

$TotalHours = ($TotalTime/(60*60));

This will most likely result in a number that is a long decimal.  To get only the whole number of hours (in other words remove the decimal point) we employ the floor() function like this:

floor($TotalTime/(60*60)) -or- floor($TotalHours)

The next step is to get the minutes.  The minutes (and the seconds as well for that matter) are in the decimal portion of the hours.  So what we need to do is subtract the whole number of hours from the value with a decimal, which leaves us only the decimal:

$DecimalMinutes = $TotalHours - floor($TotalHours);

Now to convert that decimal to a whole number of minutes we need to multiply this number by 60 (the number of minutes in an hour), and this will leave us with another number with a decimal:

$Tminutes = ($DecimalMinutes*60);

As with the hours, the whole number here are the minutes and the decimal are the seconds, so we’ll need to remove the whole number of minutes from this total like this:

$Tseconds = $Tminutes - floor($Tminutes);

The result of this should be a decimal number which lastly needs to be converted to a whole number of seconds by multiplying by 60 (number of seconds in a minute):

($Tseconds*60)

So putting it all together now, the total time elapsed on the project is:

floor($TotalHours) : floor($Tminutes) : $Tseconds

It’s a lot to read and comprehend but here is the entire bit of code to make this conversion from seconds to hours:minutes:seconds…

<?php
$TotalHours = ($TotalTime/(60*60));
$DecimalMinutes = $TotalHours - floor($TotalHours);
$Tminutes = ($DecimalMinutes*60);
$Tseconds = $Tminutes - floor($Tminutes);
echo floor($TotalHours).' : '.floor($Tminutes).' : '.($Tseconds*60);
?>

Hope this will be useful to someone.  All comments and shortcuts or better methods are welcomed and encouraged.

Follow comments on this post with this RSS 2.0 feed.
Leave a comment, or trackback from your own site.

You must be logged in to post a comment.

. . :   design & hosting by creed3.com   : . .