PHP - issue in adding 30 min to time

I am facing a weird issue, what I am trying to do here is to add 30 minutes to a specified time for multiple iterations using a while loop. Below is the code I am trying and not sure where it has gone wrong.

My Code

function session_slot_compare(){
$min_count = 3;
$time_now  = '1:30';
$time_now_new = date('H:i', strtotime($time_now));
 $duration_bits[0] = $time_now_new;
$i=0;
while($i<$min_count){
    $time_now_new = date("H:i", strtotime('+30 minutes', $time_now_new));
    $duration_bits[$i] = $time_now_new;
    $i++;
}
print_r($duration_bits);}

Expected Output

I am actually expecting the output to be like Array ( [0] => 01:30 [1] => 02:00 [2] => 02:30 )

Actual Output

But I am getting the output as Array ( [0] => 01:30 [1] => 00:30 [2] => 00:30 )

Answers


The reason is that in the strtotime('+30 minutes', $time_now_new) you pass not a valid 2-nd argument. It should be a timestamp, but in your case it's a string. The shortest way to fix the problem is to add one more strtotime() call, like the following:

$time_now_new = date("H:i", strtotime('+30 minutes', strtotime($time_now_new)));

It'll work exactly in the way you expect.


Need Your Help

Declaring functions in JS: Named and Unamed expression

javascript anonymous-function function-expression

Recentley I began looking into the JS library Typeahead as a solution to a predictive search box. However upon looking at the code to implement it I began to look into things a little deeper and it...

Calling a function within a Nested Object

javascript tidesdk

I have an object, nested within a parent object. The inner object has 2 functions and one needs to call the other. I assumed I could call this.theFunction(), but that doesn't seem to be the case.