在php中添加两个或多个时间字符串

1 php time datetime date

我有一个有时间的数组(字符串),例如"2:23","3:2:22"等.

$times = array("2:33", "4:2:22", "3:22") //loner
Run Code Online (Sandbox Code Playgroud)

我想找到所有数组的总和.

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

谢谢

Amb*_*ber 5

您可能想要查看PHP日期/时间函数 - 一个选项是使用类似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"
Run Code Online (Sandbox Code Playgroud)