sri*_*_wb 8 php comparison datetime
我有时间保存在数据库中,如下午7:30作为varchar字段.我想检查这个时间是否大于现在的时间.
我将DB时间字符串转换为'19:30',现在我想做这样的事情:
$my_time = '19:30';
if($my_time > date('H:i'))
{
do something ...
}
Run Code Online (Sandbox Code Playgroud)
问题是如果$ my_time是非空字符串,则上面将返回true.
做strtotime($my_time)也没有帮助.
strtotime('H:i',$my_time) 00:00.
这样做(int)date('H:i')会给1700年的实际时间是17:09,因此删除冒号,然后比较不会太工作....
在此上下文中,更改数据库时间数据是不可能的.
请帮助.如果我说错了一些事实,请纠正我.
Wou*_*r J 11
你可以用这个:
$myTime = '19:30';
if (date('H:i') == date('H:i', strtotime($myTime))) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
您可以构造一个新的DateTime对象,将时间设置为随机日期。比比较这两个对象。例如:
$my_time = new DateTime('January 1th 1970 19:30');
$comparable_time = new DateTime('January 1th 1970 '. date('H:i'));
if($my_time < $comparable_time) {
// do something
} else {
// do something else
}
Run Code Online (Sandbox Code Playgroud)
请注意变更日志;
Version 5.2.2 DateTime object comparison with the comparison operators changed to work as expected. Previously, all DateTime objects were considered equal (using ==).
Run Code Online (Sandbox Code Playgroud)