我是PHP的新手,我正在开发一个非常小的可视化项目 从我们的自动牌照读取器(Openalpr)到我们的叉车的车牌,以显示已到达装载区(第2区)的巫婆
一切都在Ubuntu 16.04.1上运行
我遇到的问题是MySQL数据库中读取的铭牌的时间戳是UTC时间格式:2018-04-05 08:56:33.320000
我希望它在瑞典本地时间没有毫秒(CET),例如:2018-04-05 10:56:33
我该怎么做?所有帮助表示赞赏!!
我的PHP代码:
<?php
require_once 'dbconfig.php';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = 'SELECT epoch_time_start,
best_plate
FROM front_alprgroup
ORDER BY epoch_time_start DESC LIMIT 30';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Lorrys in Zone 2</title>
</head>
<body>
<div id="container">
<h1>Lorrys in Zone 2</h1>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Lorry</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($row['best_plate']) ?></td>
<td><?php echo htmlspecialchars($row['epoch_time_start']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
</div>
</html>
网络服务器上的“Fancy”HTML输出: Lorrys in Zone 2 on webpage
答案 0 :(得分:1)
你走了:
$dateTime = '2018-04-05 08:56:33.320000';
$utc = 'UTC';
$swedish = 'Europe/Stockholm';
$newDateTime = new DateTime($dateTime, new DateTimeZone($utc));
$newDateTime->setTimezone(new DateTimeZone($swedish));
echo $newDateTime->format('Y-m-d H:i:s');
所以我通过将上面的代码添加为函数来更新代码块,然后使用该函数在循环中显示日期。
<?php
require_once 'dbconfig.php';
function convertDate($dateTime){
$utc = 'UTC';
$swedish = 'Europe/Stockholm';
$newDateTime = new DateTime($dateTime, new DateTimeZone($utc));
$newDateTime->setTimezone(new DateTimeZone($swedish));
return $newDateTime->format('Y-m-d H:i:s');
}
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = 'SELECT epoch_time_start,
best_plate
FROM front_alprgroup
ORDER BY epoch_time_start DESC LIMIT 30';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Lorrys in Zone 2</title>
</head>
<body>
<div id="container">
<h1>Lorrys in Zone 2</h1>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Lorry</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($row['best_plate']) ?></td>
<td><?php echo convertDate($row['epoch_time_start']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
</div>
</html>