【问题标题】:add two or more time strings in php在php中添加两个或多个时间字符串
【发布时间】:2010-11-25 02:45:01
【问题描述】:

我有一个带有时间(字符串)的数组,例如“2:23”、“3:2:22”等。

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

我想求所有数组的总和。

有没有办法可以添加像“2:33”和“3:33”(“i:s”)这样的时间

谢谢

【问题讨论】:

  • 对“2:33”和“3:33”求和的预期结果是什么? “5:6”(例如,5 分 6 秒)?
  • 6:06 或 6 分 6 秒

标签: php datetime date time


【解决方案1】:

没有内置方法可以做到这一点 - 所有时间函数都按时运行, 不在持续时间上。在您的情况下,您可以explode() 并添加部件 分别地。我建议写一堂课。简单例子:

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

【讨论】:

    【解决方案2】:

    所以您想为时间添加持续时间,即为时间添加小时数、分钟数和秒数?

    也许您可以使用 Dav 的 strtotime 来获取从午夜开始的秒数,然后使用 sscanf 添加持续时间,因此:

    $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);

    无法从我所在的地方测试这个,所以请原谅任何语法错误......只是想举个例子。当然,您还需要遍历您的数组。

    【讨论】:

      【解决方案3】:

      您可能想查看the PHP date/time functions - 一种选择是使用类似 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"
      

      【讨论】:

      • 21960 正好是 6 小时 6 分钟 (6*3600 + 6*60),这是正确的答案。
      • 你是对的,但在我的情况下 2:33 是 2 分 33 秒
      • 然后在前面加上'0:',或者使用mktime()从字符串中解析出来后单独指定每个字段。
      猜你喜欢
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 2013-03-09
      相关资源
      最近更新 更多