2016-11-24 7 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のエラーを取得しています:

は、ここに私のコードです。

私は12:0012:1512:30のように、各オプションは15分の段階であることを<select>要素を表示します。

+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

説明:strtotimeの出力をすべてfor loopに格納するために配列を使用しました。毎回同じ$current_time[0]を使用して、$current_time[$i]を新たに計算し、$stepの変数を+15ずつ増やしています。

+0

ありがとうたくさんの男..... –

+0

あなたは大歓迎です! – Yeti82

関連する問題