Total time help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ccrevling
Forum Newbie
Posts: 19
Joined: Mon Aug 06, 2007 1:34 pm

Total time help

Post 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.
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Total time help

Post 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.
ccrevling
Forum Newbie
Posts: 19
Joined: Mon Aug 06, 2007 1:34 pm

Re: Total time help

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Total time help

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ccrevling
Forum Newbie
Posts: 19
Joined: Mon Aug 06, 2007 1:34 pm

Re: Total time help

Post 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?
ccrevling
Forum Newbie
Posts: 19
Joined: Mon Aug 06, 2007 1:34 pm

Re: Total time help

Post 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!
Last edited by ccrevling on Wed Mar 05, 2008 11:37 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Total time help

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply