这是我的功能:
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;
}
?>
结果符合预期:
$ 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
如果我使用当前美国中部时间2015-12-26 23:39:10,则结果相同。您能否使用上述类似的存根检查结果?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句