Page 1 of 1

Total time help

Posted: Fri Feb 29, 2008 5:21 pm
by ccrevling
Mod | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


ok so heres what im doing now... sorry i post with so much help needed.. i just suck
ne way im making a clock in system and i have everything good except for one thing.. thats right i am having problems with total times

so lets say i clocked in at
1:05:00 pm
and clocked out at
2:00:00 pm
so i worked for 55 mins
the system i made for it isnt complete cause it would just parse out
-5 Min instead of 55 M ... here is my code

Code: Select all

 
<?php            
              ///////////// It runs out of my database --- So i use explodes and i use 24 hour times
      $explodeS = explode(":", $row['fullTimeS']); 
      $explodeF = explode(":", $row['fullTimeF']);
        if($explodeF[0] - $explodeS[0]==0){
        $first = "";
        }else{
        $first = $explodeF[0] - $explodeS[0]."<b>H</b> ";
        }
        if($explodeF[1] - $explodeS[1]==0){
        $second = "";
        }else{
        $second = $explodeF[1] - $explodeS[1]."<b>M</b> ";
        }
        if($explodeF[2] - $explodeS[2]==0){
        $third = "";
        }else{
        $third = $explodeF[2] - $explodeS[2]."<b>S</b> ";
        }
        echo $first.$second.$third;
      ?>
 

Mod | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Total time help

Posted: Fri Feb 29, 2008 8:34 pm
by Sekka
How exactly are the times stored in the DB? DATETIME format or just TIME? This should work for both of those.

Code: Select all

<?php
 
// Work out the differene between start and finish in seconds
$totaltime_seconds = strtotime ($row['fullTimeF']) - strtotime ($row['fullTimeS']);
 
// Display the total time in HH:MM:SS format
$totaltime_formatted = date ("H:i:s", $totaltime_seconds-3600);
 
?>
The reason I do -3600 (-1 hour) on line 7 was to make the answer correct on my machine. This may be different on yours.

Re: Total time help

Posted: Tue Mar 04, 2008 1:57 pm
by ccrevling
So the format is time not datetime or timestamp

and i used the code you posted instead of my code (BUT CHANGED IT A LITTLE FROM [ $totaltime_seconds = strtotime ($row['fullTimeF']) - strtotime ($row['fullTimeS']); ] TO [ $totaltime_seconds = strtotime ($row['fullTimeS]) - strtotime ($row['fullTimeF']); ] ) and i get this

fullTimeS (starting time) - 09:41:57
fullTimeF (finish time) - 10:03:45
came out to Total Time = 14:38:12

i will try to come up with a way to get urs to work with mine but if you can help me more than that would be great!
thanks

Re: Total time help

Posted: Tue Mar 04, 2008 2:14 pm
by pickle
You should really store the time as either a MySQL date stamp (which will allow you to use MySQL's date functions to figure this out), or a UNIX timestamp, which will let you use PHP & simple math to figure it out. Proprietary date formats are almost never a good idea.

Re: Total time help

Posted: Tue Mar 04, 2008 3:24 pm
by ccrevling
ok so i changed them to datetime
but i still get the same answer for the equation..

Code: Select all

 
# // Work out the differene between start and finish in seconds
 $totaltime_seconds = strtotime ($row['fullTimeF']) - strtotime ($row['fullTimeS']);
#  
# // Display the total time in HH:MM:SS format
$totaltime_formatted = date ("H:i:s", $totaltime_seconds-3600);   
echo $totaltime_formatted;
 
The echo equals this:
fullTimeS = 8:00:11
fullTimeF = 8:10:34
Total = 15:10:23

So what i can see is it is setting 15 as 0 so this is my full code for display

Code: Select all

 
# // Work out the differene between start and finish in seconds
 $totaltime_seconds = strtotime ($row['fullTimeF']) - strtotime ($row['fullTimeS']);
#  
# // Display the total time in HH:MM:SS format
$totaltime_formatted = date ("H:i:s", $totaltime_seconds-3600);   
 
      $explode = explode(":", $totaltime_formatted); 
        if($explode[0]==15){
        $first = "0 <b>H</b>";
        }else{
        $first = $explode[0]."<b>H</b>&nbsp;";
        }
        if($explode[1]==15){
        $second = "0 <b>H</b>";
        }else{
        $second = $explode[1]."<b>M</b>&nbsp;";
        }
        if($explode[2]==15){
        $third = "0 <b>H</b>";
        }else{
        $third = $explode[2]."<b>S</b>&nbsp;";
        }
        echo $first.$second.$third; 
 
and it works but when i go over an hour i get 16 H instead of 1 H... how can i correct this?

Re: Total time help

Posted: Tue Mar 04, 2008 3:33 pm
by ccrevling
Ok so i think i got it!

what id did was added up the hours (3600) to make the basis time not be 15 and came up with -57600 instead of -3600 and now i have the right hours displayed!

Thank you guys so much for all your help!

Re: Total time help

Posted: Tue Mar 04, 2008 3:40 pm
by pickle
Look into SUBTIME and other MySQL date functions to see if you can do all this logic in the query - save you some time & clock cycles.