How to add multiple time in php

Working with dates and times can be tricky unless you use some handy built in functions, I found myself needing to do a few different time calculations that had me stumped for awhile [head down in shame!].

I have a series of times in the format of hh:mm:ss that I needed to add together to get a total. I first tried adding them all together of course the result was wrong so I took to Google trying to find a quick solution to my problem, as this can be much quicker at times.

I found lots of results most of which had line after line of code! way too much for such a simple task, I wanted something simple then I realised as I already had the times in a time format I can make use of strtotime which will convert the time from hours, minutes and seconds to seconds then I can add those seconds together.

Here is my very simple code in action:

$total = strtotime[$r->mon] + strtotime[$r->tue] + strtotime[$r->wen] + strtotime[$r->thu] + strtotime[$r->fri];

The strtotime function expects a data to be passed you can also pass just a time which is what I have done in this case, then to show the total time I used date combined with strtotime:

date['H:i', $total]

A very simple and easy to expand solution.

Adding times together can be complex when taking seconds, minutes and hours into account of multiple time calculations add an array into the mix its even more complicated! 

This small class below enables multiple times to be added together no matter how many they are.

The class:

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 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 % 24;
            $this->totaltime = $hours.":".$minutes.":".$seconds;
        }
    }

    public function get_total_time[]{
        return $this->totaltime;
    }

}

Usage Example:

$times = array[
    '00:30:00',
    '01:15:00',
    '06:40:20',
    '02:05:16'
];

$counter = new times_counter[$times];
echo $counter->get_total_time[];

//outputs:
10:30:36

This works by passing the constructer an array of times, then as long as the passed item is an array a for loop is performed to get the hours, minutes and seconds from all the times in the array. The time calculates are performed converting by 60 seconds and minutes appropriately and finally hours by 24.

A very handy class for taking the pain out of time calculations and 'just working'.

[PHP 4, PHP 5, PHP 7, PHP 8]

str_repeatRepeat a string

Description

str_repeat[string $string, int $times]: string

Parameters

string

The string to be repeated.

times

Number of time the string string should be repeated.

times has to be greater than or equal to 0. If the times is set to 0, the function will return an empty string.

Return Values

Returns the repeated string.

Examples

Example #1 str_repeat[] example

The above example will output:

See Also

  • for
  • str_pad[] - Pad a string to a certain length with another string
  • substr_count[] - Count the number of substring occurrences

Damien Bezborodov

13 years ago

Here is a simple one liner to repeat a string multiple times with a separator:



Example script:


Example Output:
bar,bar,bar,bar,bar
?,?,?

Anonymous

10 years ago

hi guys ,
i've faced this example :


so , the length should be 35x2 = 70 !!!
if we echo it :



be carefull with characters and try to use mb_* package to make sure everything goes well ...

claude dot pache at gmail dot com

13 years ago

Here is a shorter version of Kees van Dieren's function below, which is moreover compatible with the syntax of str_repeat:

Alexander Ovsiyenko

4 years ago

//php.net/manual/en/function.str-repeat.php#90555

Damien Bezborodov , yeah but execution time of your solution is 3-5 times worse than str_replace.

Chủ Đề