【问题标题】:retrieve Datetime object from mysql database in php在 php 中从 mysql 数据库中检索 Datetime 对象
【发布时间】:2018-07-27 22:09:39
【问题描述】:

我有一个关于 mysql Datetime 格式将日期和时间一起存储在表中的特定问题,所以我已经在表中存储了值,现在我想检索这些值,然后将该时间与当前日期时间进行比较,但事情是大错特错。 这是我到目前为止所尝试的..

<?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 进行比较,这是我得到的错误“数组([matchTime] = > 2018-07-27 00:00:00 ) 数组 ( [matchTime] => 2018-07-27 00:00:00 ) 数组 ( [matchTime] => 2018-07-28 01:00:00 ) 致命错误:未捕获的异常 'Exception' 带有消息 'DateTime::__construct(): 无法在位置 0 (s) 解析时间字符串(从 fixture_list 中选择 matchTime):在数据库中找不到时区'"

【问题讨论】:

    标签: php mysql datetime


    【解决方案1】:

    这一行:

    $target = new DateTime($query);
    

    应该是:

    $target = new DateTime($row['matchTime']);
    

    【讨论】:

    • 如何在行内的matchTime中使用where子句??
    • 我不确定你的意思?
    • 我的意思是表有多行,但我想一次检索任何特定行的日期时间,所以我必须应用一些约束,比如 where 子句
    • 这不就是你对where value1='Mumbai' 所做的吗?或者您可能想要where matchTime &gt; NOW() - INTERVAL 7 DAY 之类的东西?
    • 是的,我在做什么,其中 value1='mumbai',这行得通吗??
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2016-03-29
    • 1970-01-01
    • 2016-09-16
    相关资源
    最近更新 更多