2010-05-04 134 views
1

此代碼有幾個部分。第一部分是創建燈具。Foreach時間循環不起作用

$totalRounds = $teams - 1; 
    $matchesPerRound = $teams/2; 
    $rounds = array(); 
    $roundDates = array(); 
    $curTime = time(); 

    for ($i = 0; $i < $totalRounds; $i++) { 
     $rounds[$i] = array(); 
     $numDays = $i * 4; 
     $roundDates[$i] = strtotime("+".$numDays." days",$curTime); 

    } 

    foreach($roundDates as $time) { 
     for ($round = 0; $round < $totalRounds; $round++) { 
      for ($match = 0; $match < $matchesPerRound; $match++) { 
       $home = ($round + $match) % ($teams - 1); 
       $away = ($teams - 1 - $match + $round) % ($teams - 1); 
       // Last team stays in the same place while the others 
       // rotate around it. 
       if ($match == 0) { 
        $away = $teams - 1; 
       } 

       $rounds[$round][$match] = "$user[$home]~$team[$home]@$user[$away]~$team[$away]~$time"; 
      } 
     } 
    } 

在上面的代碼中,每一次固定裝置都會有一段時間。 在代碼的末尾添加了$ time。

 for ($i = 0; $i < count($rounds); $i++) 
    { 
     foreach ($rounds[$i] as $r) 
     { 
      $seperateUsers = explode("@",$r); 
      $homeinfo = $seperateUsers[0]; 
      $awayinfo = $seperateUsers[1]; 
      $homedetails = explode("~",$homeinfo); 
      $awaydetails = explode("~",$awayinfo); 
      $database->addFixtures($homedetails[0], $homedetails[1], $awaydetails[0], $awaydetails[1], $awaydetails[2]); 
     } 
    } 

這段代碼使用分解代碼從上面放到表中。出於某種原因,輸入數據庫的日期出現在0000-00-00 00:00:00。

有沒有人可以看到解決這個問題?

編輯:循環不工作? 我在實際循環中遺漏了什麼?

回答

1

聽起來像$ homedetails [0]等沒有設置。在調用addFixtures()之前,您可以使用調試器來檢查這些值,或通過在調用之前添加簡單的echovar_dump()聲明來檢查它們。

+0

除了$ awaydetails [2]之外,所有內容都將觸及數據庫。 數據庫中的該字段設置爲日期時間。 我認爲這可能不是需要的? 上述代碼中我的日期格式將進入數據庫中? – sark9012 2010-05-04 13:47:54

+0

以這種格式「0000-00-00 00:00:00」。那就是'YYYY-MM-DD HH:mm:ss'。 strtotime()會導致自「unix時代」以來的秒數。使用PHP的'date()'函數將它放到上面的表單中。我相信,日期(「Y-m-d H:i:s」,$ time)'應該有效。 – 2010-05-04 17:20:23