add two or more time strings in php

前端 未结 3 1200
后悔当初
后悔当初 2020-12-22 02:31

I have an array with times (string) e.g \"2:23\", \"3:2:22\" etc.

$times = array(\"2:33\", \"4:2:22\", \"3:22\") //loner

I want to find the

相关标签:
3条回答
  • 2020-12-22 02:58

    So you want to add a duration to a time, meaning add a number of hours, minutes and seconds to a time?

    Perhaps you might use Dav's strtotime to get the seconds from midnight for the time, then add the duration using an sscanf, thus:

    $midnight = strtotime("0:00");
    
    // ssm = seconds since midnight
    $ssm1 = strtotime("2:33") - $midnight;
    
    // sscanf the time string... you might need to include the possibility of seconds
    list($hour, $minute) = sscanf("3:33", "%d:%d");
    
    // a
    $answer_ssm = $ssm + $hour * 3600 + $minute * 60;
    
    echo strftime("%H:%M:%S", $answer_ssm);

    Can't test this from where I am so pls excuse any syntax errors... just trying to give an example. You'll also need to iterate across your array of course.

    0 讨论(0)
  • 2020-12-22 03:04

    You might want to look at the PHP date/time functions - one option would be to use something like strtotime():

    $midnight = strtotime("0:00");
    
    // ssm = seconds since midnight
    $ssm1 = strtotime("2:33") - $midnight;
    $ssm2 = strtotime("3:33") - $midnight;
    
    // This gives you the total seconds since midnight resulting from the sum of the two
    $totalseconds = $ssm1 + $ssm2; // will be 21960 (6 hours and 6 minutes worth of seconds)
    
    // If you want an output in a time format again, this will format the output in
    // 24-hour time:
    $formattedTime = date("G:i", $midnight + totalseconds);
    
    // $formattedTime winds up as "6:06"
    
    0 讨论(0)
  • 2020-12-22 03:19

    There is no builtin way of doing this - all time functions operate on times, not on durations. In your case, you can explode() and add the parts separately. I would recommend writing a class. Simple example:

    class Duration {
    
        public static function fromString($string) {
            $parts = explode(':', $string);
            $object = new self();
            if (count($parts) === 2) {
                $object->minutes = $parts[0];
                $object->seconds = $parts[1];
            } elseif (count($parts) === 3) {
                $object->hours = $parts[0];
                $object->minutes = $parts[1];
                $object->seconds = $parts[2];
            } else {
                // handle error
            }
            return $object;
        }
    
        private $hours;
        private $minutes;
        private $seconds;
    
        public function getHours() {
            return $this->hours;
        }
    
        public function getMinutes() {
            return $this->minutes;
        }
    
        public function getSeconds() {
            return $this->seconds;
        }
    
        public function add(Duration $d) {
            $this->hours += $d->hours;
            $this->minutes += $d->minutes;
            $this->seconds += $d->seconds;
            while ($this->seconds >= 60) {
                $this->seconds -= 60;
                $this->minutes++;
            }
            while ($this->minutes >= 60) {
                $this->minutes -= 60;
                $this->hours++;
            }
        }
    
        public function __toString() {
            return implode(':', array($this->hours, $this->minutes, $this->seconds));
        }
    
    }
    
    $d1 = Duration::fromString('2:22');
    $d1->add(Duration::fromString('3:33'));
    echo $d1; // should print 5:55
    
    0 讨论(0)
提交回复
热议问题