sql - Adding two times together in php when value is greater than 24

671

I'm trying to add two time variables together, and store this new value in field called timetaken in a SQL table, which works fine unless the total is over 24 hours, in which case it resets. From what I've read and tried so far, this is because it carries over into a day. However I've not managed to come up with a solution.

$timeTakenSoFar is the currenttimetaken field from the SQL table.$timeTaken is taken from a form, which the user inputs in the format00:00:00

$time = strtotime($timeTakenSoFar) + strtotime($timeTaken) - strtotime('00:00:00');
$totalTimeTaken = date('H:i:s', $time);

$stmt = $connection->prepare("UPDATE readings SET timeTaken=? WHERE id=?");
$stmt->bind_param("si", $totalTimeTaken, $id2);
$stmt->execute();
$stmt->close();

Thank you in advance for your help!

647

Answer

Solution:

Here is from another posted, but modified to go past 24 hrs.

$time1="24:00:00";
    $time2="00:45:00";
    class times_counter {
        private $hou = 0;
        private $min = 0;
        private $sec = 0;
        private $totaltime = '00:00:00';
        public function __construct($times){
             if(is_array($times)){

                $length = sizeof($times);

                for($x=0; $x <= $length; $x++){
                        $split = explode(":", @$times[$x]); 
                        $this->hou += @$split[0];
                        $this->min += @$split[1];
                        $this->sec += @$split[2];
                }

                $seconds = $this->sec % 60;
                $minutes = $this->sec / 60;
                $minutes = (integer)$minutes;
                $minutes += $this->min;
                $hours = $minutes / 60;
                $minutes = $minutes % 60;
                $hours = (integer)$hours;
                $hours += $this->hou ;
                $this->totaltime = $hours.":".$minutes.":".$seconds;
            }
        }

        public function get_total_time(){
            return $this->totaltime;
        }

    }

    $times = array(
        '00:32:00',
        '25:15:00',
        '25:40:20',
        '02:05:16'
    );

    $counter = new times_counter($times);
    echo $counter->get_total_t//outputs:
    //10:30:36ime();`

People are also looking for solutions to the problem: php - Laravel 5.0 Cartalyst Sentinel Setup

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.