Pravin Vavadiya Pravin Vavadiya - 1 year ago 81
PHP Question

Use static keyword in a for loop?

I want to use the

keyword in my for loop.

Is it possible? if yes then how?

Here is my code:

$current_time = date('h:i A');

for($i = 0; $i <= 5; $i++) {
if ($i=0) {
echo "<option>" . date("h:i A", $current_time) . "</option>";
static $tNow = strtotime("+15 minutes",strtotime($current_time));
echo "<option>" . date("h:i A", $tNow) . "</option>";

When I'm using the
keyword, I'm getting a php error.

I want to display a
element with each option being 15 minutes steps, like

Answer Source

I don't know why you want to use static keyword in your code, but as suggested you should read here:, how and when to use static keyword.
If your goal is to display dropdown and getting option value different 15mint time like this 12:00, 12:15, 12:30..... this is my solution:

$current_time[0] = date('h:i A');
echo "<option>" . $current_time[0] . "</option>";
for($i = 1; $i <=5; $i++) {
    $current_time[$i] = strtotime("+".$step." minutes", strtotime($current_time[0]));
    echo "<option>" . date("h:i A", $current_time[$i]) . "</option>";


Working code here:

Explanation: I used an array to contain all strtotime output in for loop. Each time I used the same $current_time[0] to calculate new $current_time[$i] with a $step variable incremented by +15 each time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download