【发布时间】:2016-09-10 23:05:34
【问题描述】:
下面的代码创建一个循环,根据时间对循环进行排序,然后回显该循环的结果,直到没有更多时间循环,然后它停止。
代码运行良好,但我似乎无法根据用户时区调整时间。当我尝试时,它似乎只显示 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 小时格式的时间来回显? /p>
【问题讨论】: