用PHP中的整数乘以时间字符串(HH:MM)

时间:2015-05-13 09:31:49

标签: php datetime time date-arithmetic

我希望将2个变量相乘,以便其中一个变量采用小时格式(hh:mm), 我怎么能这样做?我得到的答案是0:

<?php
$num = 5;
$timeUnit = "00:01";

$waitingTime = $num * $timeUnit;  
echo $waitingTime;

我需要在输出中得到00:05(5分钟),但我得到0。

4 个答案:

答案 0 :(得分:3)

希望这会对你有所帮助

$num=100;
$timeUnit="00:02";
$timeUnit;
$time=explode(":",$timeUnit);
$waitingTime=$num*$time[1];
$minute = sprintf("%02d", ($waitingTime%60));
$hour = sprintf("%02d", ($waitingTime/60));
$current_time = floor($hour).':'.($minute);
echo $current_time;

答案 1 :(得分:0)

$num=20;
$timeUnit="00:03";
$time=explode(":",$timeUnit);
$waitingTime=$num*$time[1];
$sec=$time[0]+$waitingTime%60;
$hour=floor($waitingTime/60);
$timeUnit1="".$hour.":".$sec."";
$current=strtotime($timeUnit1); //seconds of current time
echo date("H:i",$current);

你可以这样做

答案 2 :(得分:0)

我最终将时间转换为秒,进行乘法并将其转换为&#34;时间格式&#34;再次:

$num=200;
$timeUnit="00:01:00";
$time=explode(":",$timeUnit);
$sec=$time[1]*60;
$sec=$sec*$num;

$diff = $sec;
$format = sprintf('%02d:%02d:%02d', ($diff / 3600), ($diff / 60 % 60), $diff % 60);
echo $format;

感谢您的帮助

答案 3 :(得分:-3)

你不能用这种方式计算字符串和数字。

你所要做的就是创建一个能够实现你想要的行为并使用它的函数/方法