我有一个关于 mysql 日期时间格式的特定问题,将日期和时间一起存储在表中,所以我在表中存储了值,现在我想检索这些值,然后将该时间与当前日期时间进行比较,但事情是出了大错。 这是我到目前为止所尝试过的..
<?php
date_default_timezone_set('Asia/Calcutta');
require_once('connect.php');
$query="select matchTime from fixture_list where value1='Mumbai'";
if($result= mysqli_query($con,$query)){
while($row=mysqli_fetch_assoc($result)){
print_r($row);
}
mysqli_free_result($result);
}
//$target = new DateTime('2018-07-27 01:00:00');
$target = new DateTime($query);
$now = new DateTime;
$diff = $target->diff($now);
if ($target > $now) {
echo $diff->format('%a days, %h hours, %i minutes to go');
}
else {
echo $diff->format('%a days, %h hours, %i minutes too late');
}
?>
我唯一的目的是获取可能已存储在数据库中的字符串中的日期时间值,并将其传递给 $target 变量,然后与 $now 进行比较,这是我收到的错误“Array ( [matchTime] = > 2018-07-27 00:00:00 ) 数组 ( [matchTime] => 2018-07-27 00:00:00 ) 数组 ( [matchTime] => 2018-07-28 01:00:00 ) fatal error :未捕获异常“异常”,消息为“DateTime::__construct(): 无法解析位置 0 (s) 处的时间字符串(从 Fixture_list 中选择 matchTime):在数据库中找不到时区””
最佳答案
这一行:
$target = new DateTime($query);
应该是:
$target = new DateTime($row['matchTime']);
关于php - 在php中从mysql数据库检索Datetime对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51566143/