PHP:如何将时间字符串与日期('H:i')进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10497929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: How to compare a time string with date('H:i')?
提问by sri_wb
I have time saved in database like 7:30pm as a varchar field. I want to check if this time is greater than time right now or not.
我将时间保存在数据库中,例如 7:30pm 作为 varchar 字段。我想检查这个时间是否大于现在的时间。
I converted the DB time string into '19:30' and now I want to do something like this:
我将 DB 时间字符串转换为 '19:30',现在我想做这样的事情:
$my_time = '19:30';
if($my_time > date('H:i'))
{
do something ...
}
The problem is the above will return always true if $my_time is non-empty string.
问题是,如果 $my_time 是非空字符串,则上述内容将始终返回 true。
doing strtotime($my_time)is not helping either.
做strtotime($my_time)也无济于事。
strtotime('H:i',$my_time)makes it 00:00 .
strtotime('H:i',$my_time)使它 00:00 。
doing (int)date('H:i')will give 1700 when the actual time is 17:09, so removing colon and then comparing will not work too ....
这样做(int)date('H:i')会在实际时间为 17:09 时给出 1700,因此删除冒号然后比较也不起作用......
Changing database time data is out of question in this context.
在这种情况下,更改数据库时间数据是不可能的。
plz help. Correct me if I stated some facts wrong.
请帮忙。如果我陈述了一些错误的事实,请纠正我。
回答by Wouter J
You can use this:
你可以使用这个:
$myTime = '19:30';
if (date('H:i') == date('H:i', strtotime($myTime))) {
// do something
}
回答by giorgio
You can construct a new DateTimeobject, setting the time on a random date. Than compare those two objects. eg:
您可以构造一个新的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
}
Please take note of the changelog;
请注意变更日志;
Version 5.2.2 DateTime object comparison with the comparison operators changed to work as expected. Previously, all DateTime objects were considered equal (using ==).
回答by Jon
You can't use the comparison operators with strings like that, because when you do the strings get converted to numbers first.
你不能对这样的字符串使用比较运算符,因为当你这样做时,字符串首先被转换为数字。
For an one-liner solution, you can use strcmp:
对于单线解决方案,您可以使用strcmp:
if(strcmp($my_time, date('H:i')) == 1)
{
do something ...
}
The condition above is semantically equivalent to "if $my_time is greater than the current time", but only if the format of the strings remains consistent!It's very easy to introduce a bug in this codeif for any reason the format of $my_timedoes not directly correspond to the H:ipattern.
上面的条件在语义上等同于“如果 $my_time 大于当前时间”,但前提是字符串的格式保持一致!如果由于某种原因格式与模式不直接对应,则很容易在此代码中引入错误。$my_timeH:i
Dumbing down the values to strings is usually not the way you should be going about using dates and times. A more appropriate solution would be to use the native DateTimeclass, introduced in PHP 5.2.0 (John Conde has already given an example in his answer).
将值简化为字符串通常不是您应该使用日期和时间的方式。更合适的解决方案是使用DateTimePHP 5.2.0 中引入的本机类(John Conde 已经在他的回答中给出了一个例子)。
However, there is also one possible advantage to treating times as dumb scalar values: the results are consistent with the human perception that 01:00 is always later than 00:00. DateTimeapproaches are dependent on the local timezone and date, and might not always give you the expected results. Example:
然而,将时间视为愚蠢的标量值也有一个可能的优势:结果与人类感知一致,即 01:00 总是晚于 00:00。DateTime方法取决于当地的时区和日期,并且可能不会总是给您预期的结果。例子:
// assume we are in London
date_default_timezone_set('Europe/London');
// assume that today is March 25, 2012
$date1 = new DateTime("2012-03-25 01:00:00");
$date2 = new DateTime("2012-03-25 02:00:00");
// and...
if ($date1 == $date2) {
echo "WTF?!? Equal???";
}
The result of this test is different than what comparing some scalar representation of "01:00" and "02:00", so it's a good idea to think about what the proper semantics are for the comparison.
此测试的结果与比较“01:00”和“02:00”的一些标量表示的结果不同,因此最好考虑一下用于比较的正确语义是什么。
回答by John Conde
$date1 = DateTime::createFromFormat('H:i', $my_time1);
$date2 = new DateTime();
if ($date1 > $date2)
{
// do something
}
回答by Jivan Pal
Don't compare strings which represent timestamps. Instead, use strtotime()to convert any such strings to Unix timestamps, which are just numbers, and then compare these. You can get the Unix timestamp for the current time with time():
不要比较代表时间戳的字符串。相反,使用strtotime()将任何此类字符串转换为 Unix 时间戳,它们只是数字,然后比较它们。您可以使用以下命令获取当前时间的 Unix 时间戳time():
$my_time = '19:30';
if (strtotime($my_time) > time()) {
// do something ...
}

