我从SQL数据库提取数据到php,将它们存储在变量中并回显它们。其中一个字段是UTC中的日期/时间戳,但是当我尝试将其转换为"欧洲/马耳他"时区时,转换不会发生。
SQL查询:
SELECT EventId, EventTime, Individual, Tenant, TenantName, DeviceName, Comment, InetDb.dbo.Individuals.FirstName, InetDb.dbo.Individuals.LastName
FROM taclogdata.dbo.Event
LEFT JOIN InetDb.dbo.Tenants ON taclogdata.dbo.Event.Tenant = InetDb.dbo.Tenants.TenantId
LEFT JOIN InetDb.dbo.Individuals ON taclogdata.dbo.Event.Individual = InetDb.dbo.Individuals.IndivId
AND taclogdata.dbo.Event.Tenant = InetDb.dbo.Individuals.TenantNdx
WHERE (EventTime BETWEEN DATEADD(ss, -$timeframe, GETDATE()) AND GETDATE())
AND (Comment = 'Reader entry' OR Comment = 'Reader exit')
AND (InetDb.dbo.Individuals.IndivId = '$ListIndivId'
AND InetDb.dbo.Individuals.TenantNdx = '$ListTenantId')"
存储和回显变量的PHP循环:
while($result_row = odbc_fetch_array($sql_run)){
date_default_timezone_set("Europe/Malta");
$EventTime = $result_row['EventTime']."<br>";
$Individual = $result_row['Individual'];
$Tenant = $result_row['Tenant'];
$TenantName = $result_row['TenantName'];
$DeviceName = $result_row['DeviceName'];
$Comment = $result_row['Comment'];
$FirstName = $result_row['FirstName'];
$LastName = $result_row['LastName'];
echo '<tr>
<td class="CellContent">'.$EventTime.'</td>
<td class="CellContent">'.$Individual.'</td>
<td class="CellContent">'.$Tenant.'</td>
<td class="CellContent">'.$TenantName.'</td>
<td class="CellContent">'.$DeviceName.'</td>
<td class="CellContent">'.$Comment.'</td>
<td class="CellContent">'.$FirstName.'</td>
<td class="CellContent">'.$LastName.'</td>
</tr>';
我使用下面的语法来"手动"将日期/时间戳$EventTime转换为马耳他时间,但是这缺乏实用性,因为需要手动更新:$EventTimeString = date('Y/m/d H:i:s',strtotime($result_row['EventTime'])+7200);
经过一些研究,我已经包括了date_default_timezone_set函数,它应该将我的脚本中的所有时间转换为马耳他时区,但是当我回叫$EventTime时,它仍然在UTC。
非常感谢帮助。提前感谢。问候,J
$EventTime
作为字符串返回,您将直接输出它。我没有看到任何转变。尝试将其更改为日期并更改时区。你甚至可以把它变成一个函数:
function convertDateToDisplay($EventTime){
$utc = new DateTimeZone('UTC');
$date = new DateTime($EventTime, $utc);
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
return $date->format('Y-m-d H:i:s');
}
调用
的示例:$EventTime = '2015-07-07 16:00';
$displayTime = convertDateToDisplay($EventTime);
echo "Event time is $EventTime Display Time is $displayTime";