日期比较仅考虑时间(PHP)

日期比较仅考虑时间(PHP),php,mysql,Php,Mysql,以下是我的功能: function hasTimeElapsed($date, $time) { if (new DateTime() > new DateTime($date.' '.$time)) { return true; } else { return false; } } $date输入为2015-12-29(MySQL格式),而$time输入为04:00:00(MySQL格式) 例如,现在是3:10。如果$time是3

以下是我的功能:

function hasTimeElapsed($date, $time) {
    if (new DateTime() > new DateTime($date.' '.$time)) {
        return true;
    } else {
        return false;
    }
}
$date
输入为
2015-12-29
(MySQL格式),而
$time
输入为
04:00:00
(MySQL格式)


例如,现在是3:10。如果
$time
是3:11,它将完全忽略日期不同的事实(29,而不是27)。我如何才能准确地检查时间是否已过,包括实际日期?

这可能不是答案,而且太长,无法发表评论;我无法复制你所观察到的。以下是我正在使用的存根:

<?php
date_default_timezone_set('America/Chicago');

$tests = array(
    array('2015-12-25', '04:00:00'),
    array('2015-12-26', '04:00:00'),
    array('2015-12-27', '04:00:00'),
    array('2015-12-28', '04:00:00'),
    array('2015-12-29', '04:00:00'),
);

$now = new DateTime('2015-12-27 03:11:10');
print 'Current time: ' . $now->format('Y-m-d H:i:s') . "\n";

foreach ($tests as $dt) {
    print sprintf("%s %s => %s\n", 
        $dt[0], 
        $dt[1], 
        hasTimeElapsed($dt[0], $dt[1], $now) ? 'T' : 'F'
    );
}   

function hasTimeElapsed($date, $time, $now) {
    $supplied = new DateTime($date.' '.$time);
    return $now > $supplied;
}
?>
如果我使用当前美国中部时间2015-12-26 23:39:10,结果相同。你能用上面类似的存根检查你的结果吗

$ php test.php
Current time: 2015-12-27 03:11:10
2015-12-25 04:00:00 => T
2015-12-26 04:00:00 => T
2015-12-27 04:00:00 => F
2015-12-28 04:00:00 => F
2015-12-29 04:00:00 => F