算术运算上的Php变量增加时间

时间:2014-09-25 07:23:42

标签: php datetime

我无法在算术运算中对简单字符串进行乘法运算" *"将mineutes添加到变量,使用整数测试工作正常,任何想法。谢谢

$minu = intval("2");                                                                
$timestring = "00:02:42";

$futureTime = strtotime($timestring) + (60*2); // works fine adding two minutes
$futureTime = strtotime($timestring) + (60 * $minu); // doesn't work and returns same value

$formatTime = date("H:i:s", $futureTime);

echo $formatTime; /// 00:04:42 /// ok

//// NOW i concatenate and storage in DB like this value triming : "1".000412."000"
//// Operation no ejecute when (60 * $minu) with this output 00:02:42

4 个答案:

答案 0 :(得分:1)

结果是正确的:

   $minu = intval("2");                                                             
   $timestring = "00:02:42";

   $futureTime =  strtotime($timestring) + (60 * $minu);

   $formatTime = date("H:i:s", $futureTime);
   echo $formatTime; // 00:04:42
   echo $futureTime; //1411617882

答案 1 :(得分:0)

你的代码会像下面的代码一样产生相同的结果:我认为它没有错误

 echo date("H:i:s", strtotime("00:02:42") + (60 * intval('2')));

答案 2 :(得分:0)

轻松修复,确保变量正确;)[你正在回应错误的一个]

我个人会使用Carbon。将来你会有很多头痛的事! https://github.com/briannesbitt/Carbon

碳示例:

$minutes = 2;
$time = '00:02:42'
$carbon = Carbon::parse($time)->addMinutes($minutes);
echo $carbon->format('H:i:s');

答案 3 :(得分:0)

试试这个:

$futureTime =  strtotime($timestring) + (60 * $minu);
$formatTime= DateTime::createFromFormat('H:i:s', $futureTime );