Alejandro Beltran
Alejandro Beltran

Reputation: 327

Php variables on arithmetic operations adding time

i cannot multiplicate the simple string in arithmetic operation "*" adding mineutes to variable, testing with integers works fine, any idea. Thanks

$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

Upvotes: 1

Views: 254

Answers (4)

KernelCurry
KernelCurry

Reputation: 1297

Easy fix, make sure the variables are correct ;) [You are echoing the incorrect one]

Personally I would use Carbon. It will have a you a LOT of headache in the future! https://github.com/briannesbitt/Carbon

Carbon Example:

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

Upvotes: 0

munsifali
munsifali

Reputation: 1732

try this:

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

Upvotes: 0

Hendry Tanaka
Hendry Tanaka

Reputation: 464

Your code will be make the same result like my code below: And it make no mistake I think

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

Upvotes: 0

overals
overals

Reputation: 92

Result is correct:

   $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

Upvotes: 1

Related Questions