Author Topic: Time calculation help  (Read 424 times)

0 Members and 1 Guest are viewing this topic.

Offline vinoindiamcaTopic starter

  • Irregular
  • Posts: 29
    • View Profile
Time calculation help
« on: March 10, 2009, 03:19:07 AM »
Hi have problem of adding total number of hours and minutes in a recordset
My record is like
From time    to time          total time
9:30 AM      11:30 AM       2:00
9:15 AM      10:15 AM       1:00

----------------------------------
                                     3:00 (Not worked)
----------------------------------
I have code for adding two given time like start time and end time, but i need total number of hours worked. Please help in this.

Following is the code for two given time

$sum=AddPlayTime($tims,$tims1);// Adding two given hours and minutes like 1:30 and 2:30 gives 4:00



function AddPlayTime ($oldPlayTime, $PlayTimeToAdd) {
    $pieces = split(':', $oldPlayTime);
    $hours=$pieces[0];
    $hours=str_replace("00","12",$hours);
    $minutes=$pieces[1];
    $oldPlayTime=$hours.":".$minutes;
    $pieces = split(':', $PlayTimeToAdd);
    $hours=$pieces[0];
    $hours=str_replace("00","12",$hours);
    $minutes=$pieces[1];
    $str = $str.$minutes." minute ";
    $str = "01/01/2010 ".$oldPlayTime." am + ".$hours." hour ".$minutes." minute ";
    if (($timestamp = strtotime($str)) === false) {
        return false;
    } else {
        $sum=date('h:i', $timestamp);
        $pieces = split(':', $sum);
        $hours=$pieces[0];
        $hours=str_replace("12","00",$hours);
        $minutes=$pieces[1];
        $sum=$hours.":".$minutes;
        return $sum;
    }
}


Offline ninedoors

  • Enthusiast
  • Posts: 234
  • Gender: Male
    • View Profile
    • Barrie Men's Hockey League
Re: Time calculation help
« Reply #1 on: March 10, 2009, 07:03:16 AM »
I think you could save yourself a lot of trouble by storing your start and end times as unix timestamps, time().  In your code it looks as though you break it down and convert both times to them anyway.  So just store them in your DB that way and all you have to do is subtract endtime form starttime and use the date() function to format them the way you want.

Plus this will allow you to get rid of your date cloumn which I assume you have that holds the date of the start and end times.

Let em know if I have misunderstood what you are doing and I could take another stab at your problem if this is incorrect.

Nick
« Last Edit: March 10, 2009, 07:04:08 AM by ninedoors »

Offline vinoindiamcaTopic starter

  • Irregular
  • Posts: 29
    • View Profile
Re: Time calculation help
« Reply #2 on: March 10, 2009, 08:36:34 AM »
Thank you sir,
                   Just give me the details to add total number of hours worked. I have stored from time and to time as varchar because i need to show it like 10:30.
S.No     Date            Project       Status        From Time     To Time      Hours worked
 1    10-03-2009    Proj1       Completed      09:15 AM    11:15 AM     02:00
 2    10-03-2009    Proj2       Pending          09:25 AM    12:40 PM    03:15
 3    10-03-2009    Proj3       Pending          09:52 AM    11:38 AM    01:46
 4    10-03-2009    Proj4      Completed      09:52 AM    12:38 PM    02:46
     
Total Hours                                                                                 8 Hours

I am getting like this result here i am getting only hours added but minutes are not added.
Please help in this

My PHP Code is as below
         $queryses = "SELECT *  FROM empproject where uid='$uid'";
         $resultses=mysql_query($queryses) or die(mysql_error());
         
         $query2 = "SELECT COUNT(*) AS numrows FROM empproject where uid='$uid'";
         $result2 = mysql_query($query2) or die('Error, query failed');
         $row2 = mysql_fetch_array($result2);
         $numrows = $row2['numrows'];
         
         for ($j = 0; $j<=$numrows; $j++)
            {
               while($resses=mysql_fetch_array($resultses))
               {
                  $tot2=$resses['totalhrs'];
                  $tot1="00:00";
                         $sumss+=AddPlayTime($tot1,$tot2);// This code adds start time and end time
               }
            }




Offline ngreenwood6

  • Devotee
  • Posts: 1,349
    • View Profile
Re: Time calculation help
« Reply #3 on: March 10, 2009, 08:52:01 AM »
Look at what Ninedoors said to you. You really should store the time in the database as a unix timestamp which will give you something like (09234123412) for the time. This method allows you to perform alot of functions on it. From that you can get the date, time, week, day, and many other things. They are also alot easier to work with and do not need complex functions like the one you are a trying to create. I would convert to this system before you get too far along and have about 20 of the functions that you have there. If you need help converting I am sure anyone would be willing to help.
if($noob == true || $code_tags == false){
  echo 'Please use code tags.';
} else if($topic != $forum) {
  echo 'Please post in the correct forum.';
}

Offline ninedoors

  • Enthusiast
  • Posts: 234
  • Gender: Male
    • View Profile
    • Barrie Men's Hockey League
Re: Time calculation help
« Reply #4 on: March 20, 2009, 04:47:20 PM »
First of all drop the date column form your database.  It will only cause you problems.  Change your fromtime and totime columns from a varchars to an ints. 

I am assuming you have a form that you enter the date and time for you rstart and end time.  You can take this info you get from the form and use the mktime() function to format that info into a unix timestamp.

Then all you are going to have to do is subtract your totime form your fromtime column to get the time worked in seconds.  Then all you have to do is convert that to hours and minutes.  Let me know if you need any help.

Nick