2016-11-24 231 views
-1

我想在我的for循环中使用static关键字。在for循环中使用static关键字?

可能吗?如果是,那么如何?

这里是我的代码:

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

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

当我使用static关键字,我发现了一个PHP错误。

我想显示一个<select>元素,每个选项为15分钟步骤,如12:0012:15,12:30

+1

**你究竟是想通过这个**达到**? –

+1

但是,为什么你需要使用它? – malutki5200

+0

通过使用** static **关键字,您的目标是什么?你有没有阅读何时/在哪些情况下我们可以使用这个http://php.net/manual/en/language.oop5.static.php? –

回答

0

我不知道为什么你要在你的代码中使用static keyword,但建议你应该阅读这里:http://php.net/manual/en/language.oop5.static.php,如何以及何时使用static keyword
如果你的目标是要显示下拉列表,并获得期权价值不同15mint时间 这样的12:00,12:15,12:30 .....这是我的解决方案:

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

echo"</select>"; 
var_dump($current_time); 

工作代码这里:http://ideone.com/zM0eue

说明:我用一个数组来包含for loop中的所有strtotime输出。每次我使用相同的$current_time[0]来计算新的$current_time[$i]$step变量每次增加+15。

+0

非常感谢的人..... –

+0

不客气! – Yeti82