php - 比较mysql的时间和php中的time()

标签 php mysql datetime unix-timestamp

我需要帮助。这是链接在上午 7:00 到上午 7:30 之间可用的场景,该时间来自 mysql 数据库。

StartTime = 2014-03-19 07:00:00

EndTime = 2014-03-20 07:30:00

iv'e尝试将其转换为php中的strtotime()。

这是我的代码

$date_s = new DateTime($row->StartDate);

$date_e = new DateTime($row->EndDate);

$g = date("Y-m-d H:i:s");

<?= strtotime($date_s->format('Y-m-d H:i:s')) > strtotime($g) && strtotime($date_e->format('Y-m-d H:i:s')) < strtotime($g) ? "true" : "false"; ?>

结果总是让我失望,我大约 1 小时前就堆栈了。任何人都可以帮忙吗? :D

最佳答案

您可以直接比较 DateTime 对象:

$date_s = new DateTime($row->StartDate);
$date_e = new DateTime($row->EndDate);
$now = new DateTime();

if ($now > $date_s && $now < $date_e) {

}

或者不使用 DateTime 对象:

$date_s = strtotime($row->StartDate);
$date_e = strtotime($row->EndDate);
$now = time();

if ($now > $date_s && $now < $date_e) {

}

使用其中之一,但不能同时使用两者。

关于php - 比较mysql的时间和php中的time(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22527096/

相关文章:

mysql - 我将如何在 mysql 表中存储未知数量的数据?

c# - 使用 DateTime.ParseExact 将字符串转换为日期时间

Python-日期转换

php - 编码/解码多个国际字符的最佳方法 (MySQL/PHP)

php - 如何在同一 php 页面上发送 Jquery Ajax 数据?

mysql - 查询中同时使用 group by 和 order by 时避免创建临时表

PHP & MySQL & 表单——灵活的搜索? (例如 : X ingredients can make Y recipes)

python - 时间戳问题: Python doesn't recognize Timestamp

Php If语句取其他大于0的最小数

php - Youtube channel 嵌入尺寸