我需要计算localDatetime和从我的数据库表(csv)中名为ReportedDateAndTime的特定行获取的Datetime之间的时差。我搜索了很多解决方案,但它并没有为我工作。显示本地时间&从数据库获取的时间正在运行。但时差并不是。到目前为止,这是我的代码。请帮忙。提前谢谢!
<?php
include_once('connection.php');
$sql="SELECT * FROM csv";
$records=mysqli_query($conn, $sql);
$date = new DateTime(null, new DateTimeZone('Asia/Colombo'));
while ($csv=mysqli_fetch_assoc($records))
{
$exp = $csv['ReportedDateAndTime'];
}
$diff = $date->diff($exp);
echo $diff->format('%H')
?>
答案 0 :(得分:1)
来自mysql doc
SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
&#39; 2004-01-01 13:00:00&#39;
SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
&#39; 2004-01-01 22:00:00&#39;
https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
答案 1 :(得分:1)
谢谢大家。幸运的是我找到了答案。 :)
<?php
include 'connection.php';
date_default_timezone_set('Asia/Colombo');
$the_time = date('H:i');
echo $the_time." </br>";
$sql="SELECT ReportedDateAndTime FROM csv";
$result=$conn->query($sql);
while ($row = $result->fetch_assoc())
{
$new_time=$row['ReportedDateAndTime'];
echo "<br/>";
echo $new_time." ReportedDateAndTime</br>";
$datetime1 = new DateTime($the_time);
$datetime2 = new DateTime($new_time);
$interval = $datetime1->diff($datetime2);
$hours=$interval->format('%h');
$minutes= $interval->format('%i');
echo "<br/>";
echo $hours." Hours ".$minutes." Minutes</br></br>";
}
?>