Using a Time Zone Conversion When Dynamically Generating Results
If I understand correctly, the code below converts $row["datesubmitted"]
from one timezone to another.
I would like to print the converted $row["datesubmitted"]
dynamically in an HTML table. Is there a way that I can apply the conversion below for each row that is pulled fr开发者_如何学Goom MySQL? I assume that I can't just plug $row["dt"] into the code since there is no field called "dt" in the MySQL that I am using.
Thanks in advance,
John
$dt = new DateTime($row["datesubmitted"], $tzFrom);
$dt->setTimezone($tzTo);
Loop through your recordset creating new DateTime objects for each row:
while ($row = mysql_fetch_assoc($res)) {
$dt = new DateTime($row["datesubmitted"], $tzFrom);
$dt->setTimezone($tzTo);
echo '<tr><td>'.$dt->format('Y-m-d H:i').'</td></tr>';
}
With help from others, here is what I got to work:
$tzFrom = new DateTimeZone('America/New_York');
$tzTo = new DateTimeZone('America/Phoenix');
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) {
$dt = new DateTime($row["datesubmitted"], $tzFrom);
$dt->setTimezone($tzTo);
echo '<tr>';
echo '<td class="sitename1"><a href="http://www.'.$row["url"].'">'.$row["title"].'</a></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="sitename2name">Submitted by <a href="http://www...com/sandbox/members/index.php?profile='.$row["username"].'">'.$row["username"].'</a> on '.$dt->format('l, F j, Y &\nb\sp &\nb\sp g:i a &\nb\sp &\nb\sp \A\Z &\nb\sp \T\i\m\e').'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="sitename2"><a href="http://www...com/sandbox/comments/index.php?submission='.$row["title"].'&submissionid='.$row["submissionid"].'&url='.$row["url"].'">'.$row["countComments"].' comments</a></td>';
echo '</tr>';
}
echo "</table>";
精彩评论