php - 在php中从mysql数据库检索Datetime对象

标签 php mysql datetime

我有一个关于 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/

相关文章:

java - 使用java将7000万条记录从xml插入mysql的最快方法

python - MySQL-python 安装程序找不到正确的 Python 版本

string - 可以直接比较存储为字符串/文本的日期吗?

使用 utf8mb4 的 PHP/mysql 站点无法从数据库中正确检索表情符号,尽管在我能找到的所有地方都指定了 utf8mb4

php - PDO 子句使用 for

php - ob_start 并包含问题

c# - 我可以用欧洲符号存储我的 DateTime 吗?

php 数据库帮助,数据库托管在与 Web 主机不同的服务器上

mysql - REGEXP 不适用于字符名称

c# - 这是用 TimeSpan 表示几十年的最佳方式吗?