[英]Timezone looping issues in php
The code below creates a loop, sorts the loop based on time, and then echos the results of that loop until there is no more time left to loop, then it stops. 下面的代码创建一个循环,根据时间对循环进行排序,然后回显该循环的结果,直到没有剩余时间循环,然后停止。
The code works great, except I cannot seem to adjust the times based on user timezone. 代码工作得很好,除了我似乎无法根据用户时区调整时间。 When I try, it seems to only display 1 result, then the loop stops.
当我尝试时,它似乎只显示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++;
}
}
}
?>
To make this perfectly clear, if I remove the timezone formatting code below from the code above, the loop runs perfectly. 为了完全清楚,如果我从上面的代码中删除下面的时区格式代码,循环运行完美。 If I keep the timezone code from below, it only displays one result and breaks the loop.
如果我保留下面的时区代码,它只显示一个结果并打破循环。
$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');
What am I doing wrong? 我究竟做错了什么? How can I write this in such a way that the time is sorted based on the full server date/time stored in the database, then echo'd based on just the time in 12 hour format without date, converted from users timezone settings?
如何以这样一种方式编写,即根据存储在数据库中的完整服务器日期/时间对时间进行排序,然后根据用户时区设置转换的12小时格式的无日期回送?
Well this took me some serious amount of time to work through. 嗯,这花了我一些时间来完成工作。
The problem was as follows: 问题如下:
I have a while loop 我有一个循环
while ( $row = $stmt->fetch() )
This means if I set $row to something other than $stmt->fetch(), the while loop will end. 这意味着如果我将$ row设置为$ stmt-> fetch()以外的其他内容,while循环将结束。 I knew that from the start, hence calling the second $row1.
我从一开始就知道,因此调用第二个$ row1。
What I wasn't also realizing is that means if I used $stmt to perform other functions within the while loop, it to would break the while loop. 我没有意识到的是,这意味着如果我使用$ stmt在while循环中执行其他函数,它将打破while循环。
The solution was simply to use a different variable in the time functions. 解决方案只是在时间函数中使用不同的变量。
$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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.