【问题标题】:Time Comparison Not Working PHP时间比较不工作 PHP
【发布时间】:2013-07-09 00:22:41
【问题描述】:

我从 mysql 获取时间,格式为:hh:mm:ss 我需要将它与当前时间进行比较,但它不起作用

$result = mysql_query('SELECT * FROM events WHERE clubID = "'.$clubID.'" ORDER BY date ASC, TIME(startTime) ASC');

while ($row = mysql_fetch_assoc($result)) {

    $originaltime = $row['endTime'];
    $time = new DateTime($originaltime);

    if($time < time())
        mysql_query('UPDATE events SET passed="1" WHERE eventID = "'.$row['eventID'].'"');

}

如果当前时间已经超过$time(来自mysql),我希望if语句发生

【问题讨论】:

  • 您正在将一个对象与时间戳进行比较...
  • 你可以用一个更新查询来做到这一点,没有 php

标签: php mysql datetime compare


【解决方案1】:

尝试转换

new DateTime($originaltime)

to int(自 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)以来的秒数),所以解决方案应该是:

if ($time->format('U') < time())

【讨论】:

  • 非常感谢你是救生员
  • @nshah 甚至 $time-&gt;getTimestamp() as DateTime::format() 返回一个可能需要转换为整数的字符串。但是 DateTime::getTimestamp() 返回一个 int。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多