Ok so this issue has been stumping me for awhile. I have a website where a user inputs 2 times and dates i.e. 0100 and 0400 hose get assigned to a variable and are later pulled from the database to get the total time.
Currently I have the code below which will add the 2 times but what I am experiencing right now is the times add fine until the 12th hour at which it resets to 0. I have mysql database as a time data type and if I manually plug in lets say 45 hours it will display that amount but as soon as my function adds time to it, it resets to 0. Any advice would be greatly appreciated. The function I am using isnt mine and was a found function, I am just having ahard time wrapping my thoughts around this one. Maybe cause ive looked at it too long. :) Thank You

function AddPlayTime ($oldPlayTime, $PlayTimeToAdd) { 

    $pieces = split(':', $oldPlayTime); 
    $hours=$pieces[0]; 
    $hours=str_replace("00","12",$hours); 
    $minutes=$pieces[1]; 
    $seconds=$pieces[2]; 
    $oldPlayTime=$hours.":".$minutes.":".$seconds; 

    $pieces = split(':', $PlayTimeToAdd); 
    $hours=$pieces[0]; 
    $hours=str_replace("00","12",$hours); 
    $minutes=$pieces[1]; 
    $seconds=$pieces[2]; 

    $str = $str.$minutes." minute ".$seconds." second" ; 
    $str = "01/01/2000 ".$oldPlayTime." am + ".$hours." hour ".$minutes." minute ".$seconds." second" ; 


    if (($timestamp = strtotime($str)) === false) { 
        return false; 
    } else { 
        $sum=date('h:i:s', $timestamp); 
        $pieces = split(':', $sum); 
        $hours=$pieces[0]; 
        $hours=str_replace("12","00",$hours); 
        $minutes=$pieces[1]; 
        $seconds=$pieces[2]; 
        $sum=$hours.":".$minutes.":".$seconds; 

        return $sum; 

    } 
} 

$firstTime=$data3[flightHours]; 
$secondTime=mysql_real_escape_string($_GET['flightTime']); 

$sum=AddPlayTime($data3[flightHours], mysql_real_escape_string($_GET['flightTime']));
$sumUnit=AddPlayTime($data4[hours], mysql_real_escape_string($_GET['flightTime']));
$sumWing=AddPlayTime($rowwg[hours], mysql_real_escape_string($_GET['flightTime']));
$sumSq=AddPlayTime($rowsq[hours], mysql_real_escape_string($_GET['flightTime']));
Member Avatar for diafol

Don't use it. This found script sucks.

Use DateTime objects for safely adding time. You can use procedural or OOP varieties:

function addHoursToDT($hours,$datetime=NULL)
{
    $dt = new DateTime($datetime);
    $hFormat = 'PT' . $hours . 'H';
    $dt->add(new DateInterval($hFormat);
    return $dt->format('Y-m-d H:i:s');
}

echo addHoursToDT(45) . '<br />';
echo addHoursToDT(45, '2013-03-03 12:53:00') . '<br />';
echo addHoursToDT(45, '2013-03-03') . '<br />';
Member Avatar for diafol

Perhaps I was over-eager to use DT. You could even use mktime...

function modifyTime($increment, $inputDate=NULL, $format='Y-m-d H:i:s')
{
    $dateToUse = ($inputDate) ? strtotime($inputDate) : time(); 
    $inc = array_pad((array) $increment, 6, 0);
    list($H,$i,$s,$n,$j,$Y) = $inc;
    $ts = mktime(date('H',$dateToUse)+$H,date('i',$dateToUse)+$i,date('s',$dateToUse)+$s,date('n',$dateToUse)+$n,date('j',$dateToUse)+$j,date('Y',$dateToUse)+$Y);
    return date($format, $ts);
}

echo modifyTime(array(0,0,0,0,4),'21-09-2013');
    //add 4 days from Europe format date
echo modifyTime(array(0,0,0,0,-9),'09/21/2013');
    //sub 9 days from US format date

The first param is an array for [hours,mins, secs, months, days, years], If you just want to add or substract hours, simple:

echo modifyTime(-6,'2013-09-21 00:06:00');

Sorry for my delay, work always seems to get in the way. I will see if this works for me adn will get to you to let you know. Thank you.

Ok so I get this to run but if the user has say 23 hours logged and tries to log lets say 3 more hours it resets to zero as 2 hours instead of 26. Also if I try to add more than 24 hours this shows. 1969-12-31 20:00:00

I am submitting time in the following format H:i (00:00)

how do I keep this from happening

Member Avatar for diafol

Seems to work for me...

function modifyTime($increment, $inputDate=NULL, $format='Y-m-d H:i:s')
{
    $dateToUse = ($inputDate) ? strtotime($inputDate) : time(); 
    $inc = array_pad((array) $increment, 6, 0);
    list($H,$i,$s,$n,$j,$Y) = $inc;
    $ts = mktime(date('H',$dateToUse)+$H,date('i',$dateToUse)+$i,date('s',$dateToUse)+$s,date('n',$dateToUse)+$n,date('j',$dateToUse)+$j,date('Y',$dateToUse)+$Y);
    return date($format, $ts);
}
//add 23 hours
$first = modifyTime(array(23,0,0,0,0,0));
echo "Adding 23 hours to NOW: " . $first . "<br />";
//add 2 hours
$second = modifyTime(array(2,0,0,0,0,0), $first);
echo "Adding another 2 hours to datetime above: " . $second . "<br />";
//add 27 hours, 20 minutes
$third = modifyTime(array(27,20,0,0,0,0));
echo "Adding 27 hours and 20 minutes to NOW: " . $third;

If you are only ever going to be adding time in the H:i format, then we can adapt the function above easily...

function modifyTime($increment, $inputDate=NULL, $format='Y-m-d H:i:s')
{
    list($hr,$min) = array_map("intval", explode(":", $increment));
    $dateToUse = ($inputDate) ? strtotime($inputDate) : time(); 
    $ts = mktime(date('H',$dateToUse)+$hr,date('i',$dateToUse)+$min,date('s',$dateToUse),date('n',$dateToUse),date('j',$dateToUse),date('Y',$dateToUse));
    return date($format, $ts);
}
//add 27 hours, 20 minutes
$trial = modifyTime('27:20');
echo "Adding 27 hours and 20 minutes to NOW: " . $trial;

However, you may be better off with the DateTime solution if it's that simple. Running multiple date() functions without any need may be wasteful.

Ok I think I see where the miscommunication may be. Indeed the function works it takes today and if I add 24 hours will show tomorrow at the same time. I dont think I clearly stated my end result. I am looking for a way to add accumulated time.

Example member has accumulated 17 hours and does 9 hours of total work, I wish to display that the memebr hnow has a total of 26 hours of work, then he works again for 4 and his total should go up to 30.

I do apologize for my unclarity in what I was looking for. Ive just been so wrapped up in it that my head is about to explode. LOL

Member Avatar for diafol

I really have no idea of what you're looking for. You want to add time to an existing time?? You want to add hours/mins to hours/mins regardless of the actual time?

Correct. It could be compared to something that would show total hours logged onto a website or in a game

Here is the exact

Member has 235:30 (235 hours and 30 minutes) of flight hours. He then files a for a new flight that is lets say 3:45 (3 hours and 15 minutes). Once the hours are approved as valid then the 3:45 gets added to the 235:23 for a total of 239:15 (239 hours and 15 minutes)

It is here where I have been beating my head against the wall. I think it may have to be covnerted to an integer or seconds but Im not very well versed in time and every time and thing I have done never calcultes correctly as Im sure I am missing something small or converting wrong.

Member Avatar for diafol

That's pretty easy, but I think your maths is a bit iffy. ;)

$current = '6:00'; 


function addTime($modify, $worked='0:00')
{

    list($h,$m) = array_map("intval", explode(":",$modify));
    list($origH, $origM) = array_map("intval", explode(":",$worked));

    $totalMins = ($h + $origH)*60 + $m + $origM;

    $hrs = floor($totalMins/60);
    $min = str_pad($totalMins%60,2,"0",STR_PAD_LEFT);

    return $hrs . ':' . $min;
}



$current = addTime('2:43', $current);

$current = addTime('7:59', $current);

echo $current;

I'm sure there's an easier way to do it, but it works.

HAHAH yea I just put a generic time down and didnt update to make easy math. So one more issue has come up since this does indeed work, and sorry to be a pain, but the max time allowed for the data base is 838:59:59. Members have hours that well surpass that number. So that puts me at a stand still. Do you know of a work around for this problem.

Member Avatar for diafol

If you have access to the DB, you can change the field. What's the datatype?

Ah - it's 'time' - yes that's the limit. You can change the datatype to varchar maybe 10 chars. hhhh:mm:ss ?

I don't think it'll matter much as the function accepts times as strings.

However, I'm a bit uncomfortable with that - how about saving to DB as total minutes - so you can use 'int' datatype.

Bam that did it. I wasnt suer if the varchar would work but it seems to be doing so. I added over 1800 hours and no error. I just need to in and clean up the mess I made with all the work you helped me with.

As always I have found help here and thank you for putting up with me. I will definitly post again when I run into my next snag.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.