我需要计算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')
?>
从mysql文档
SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
2004年1月1日13:00:00
SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
2004年1月1日22:00:00
https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
谢谢大家。幸运的是,我找到了答案
<?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>";
}
?>