2016-09-10 43 views
1

下面的代碼創建一個循環,根據時間對循環進行排序,然後回顯該循環的結果,直到沒有更多時間循環,然後停止。php中的時區循環問題

該代碼很好,除非我似乎無法根據用戶時區調整時間。當我嘗試時,它似乎只顯示1個結果,然後循環停止。

while ($row = $stmt->fetch()) 
{ 
    if (!($sc > $stopcount)) 
    { 
     $i++; 
     if(date('M d, Y', strtotime($row['time'])) !== $lastTime) 
     { 
      if ($i == '1') 
      { 
        echo '</tr><tr><td colspan="6" class="heading">'. date('M d, Y', strtotime($row['time'])) .'</td></tr>'; 
      } else if ($lastTime === NULL) 
      { 
      echo '</tr>'; 
      } else 
      { echo '</tr><tr><td colspan="6" class="heading">'. date('M d, Y', strtotime($row['time'])) .'</td></tr>'; 
      } 
      $lastTime = date('M d, Y', strtotime($row['time'])); 
     } if ($i >= $sc) 
     { ?> 
      <tr> 
       <td> 
        <input type="checkbox" name="checkbox" value="<? echo $row['id']; ?>" /> 
       </td> 
       <td class="status"> 
        <? if ($row['status'] == 'unopened' || $row['status'] == 'closed') { ?> 
         <span id="<? echo $row['id']; ?>" class="icon-folder-close"></span> 
        <? } ?> 
        <? if ($row['status'] == 'opened' || $row['status'] == 'reopened') { ?> 
         <span class="icon-folder-open"></span> 
        <? } ?> 
       </td> 
       <td> 
        <? echo $row['mailfrom']; ?> 
       </td> 
       <td> 
        <strong><a href="#preview_mail" class="mails_show open" id="<? echo $row['id']; ?>" data-toggle="modal" data-show="mail-<? echo $i; ?>"><? echo $row['subject']; ?></a></strong> 
        <div id="mail-<? echo $i; ?>" class="mails_container"> 
         <div class="from"><? echo $row['mailfrom']; ?></div> 
         <div class="to"><? echo $row['mailto']; ?></div> 
         <div class="ids"><? echo $row['id']; ?></div> 
         <div class="key"><? echo $row['pin']; ?></div> 
         <div class="subject"><? echo $row['subject']; ?></div> 
         <div class="attach"><? if ($row['attachment'] !== NULL) { ?> <span class="icon-gift"></span> <a href="<? echo $row['attachment']; ?>"><? echo $row['attachment']; ?></a> <? } else { ?><span class="icon-none"></span>NONE<? } ?></div> 
         <div class="body"> 
          <p><? echo $row['message']; ?></p> 
         </div> 
         <div class="body_reply"> 
          <p><? echo $row['message']; ?></p> 
         </div> 
        </div>          
       </td> 
       <td> 
        <? 
        $tttime = $row['time']; 
        $ttime = new DateTime($tttime); 
        $stmt=$db->prepare('SELECT timezone FROM member_credits WHERE username = :user'); 
        $stmt->bindParam(':user', $username); 
        $stmt->execute(); 
        $row1 = $stmt->fetch(); 
        $usersTimezone = (new DateTimeZone($row1[timezone])); 
        $ttime->setTimeZone($usersTimezone); 
        $ttimee = $ttime->format('h:i, A'); 
        ?> 
        <? echo $ttimee; ?> 
       </td> 
       <td style="padding: 10px 10px !important;"> 
        <? if (isset($row[attachment])) { ?> 
         <span style="margin-left: 0 !important;" class="icon-gift"></span> 
        <? $bytes=filesize($row[attachment]); echo formatSizeUnits($bytes); } else {?> <? } ?> 
       </td> 
      </tr> 
     <? $sc++; 
     } 
    } 
} 
?> 

要做到這一點非常清楚,如果我從上面的代碼中刪除時區格式代碼,循環運行完美。如果我從下面保留時區代碼,它只顯示一個結果並打破循環。

$ttime = new DateTime($tttime); 

    $stmt=$db->prepare('SELECT timezone FROM member_credits WHERE username = :user'); 
    $stmt->bindParam(':user', $username); 
    $stmt->execute(); 
    $row1 = $stmt->fetch(); 

    $usersTimezone = (new DateTimeZone($row1[timezone])); 
    $ttime->setTimeZone($usersTimezone); 
    $ttimee = $ttime->format('h:i, A'); 

我在做什麼錯?我該如何編寫這種方式,以便根據存儲在數據庫中的完整服務器日期/時間對時間進行排序,然後僅基於12小時制格式的時間而不用日期進行回顯,從用戶時區設置轉換而來?

回答

0

嗯,這花了我很多時間來完成。

的問題如下:

我有一個while循環

while ($row = $stmt->fetch()) 

此,while循環將結束意味着如果我設置$行到$ stmt-其他東西>取() 。我從一開始就知道,因此調用第二行$ row1。

我還沒有意識到的是,如果我使用$ stmt在while循環內執行其他函數,它將打破while循環。

解決方案只是在時間函數中使用不同的變量。

$ttime = new DateTime($tttime); 

    $stmt2=$db->prepare('SELECT timezone FROM member_credits WHERE username = :user'); 
    $stmt2->bindParam(':user', $username); 
    $stmt2->execute(); 
    $row1 = $stmt2->fetch(); 

    $usersTimezone = (new DateTimeZone($row1[timezone])); 
    $ttime->setTimeZone($usersTimezone); 
    $ttimee = $ttime->format('h:i, A');