php PHP比较时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6158726/
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 compare time
提问by DiegoP.
How to compare times in PHP?
如何比较 PHP 中的时间?
I want to say this:
我想说的是:
$ThatTime ="14:08:10";
$todaydate = date('Y-m-d');
$time_now=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('G:i:s',$time_now);
if($NowisTime >= $ThatTime) {
echo "ok";
}
The above code does not print ok. I expected it to.
上面的代码打印不正常。我预料到了。
回答by KingCrunch
$ThatTime ="14:08:10";
if (time() >= strtotime($ThatTime)) {
echo "ok";
}
A solution using DateTime
(that also regards the timezone).
使用DateTime
(也考虑时区)的解决方案。
$dateTime = new DateTime($ThatTime);
if ($dateTime->diff(new DateTime)->format('%R') == '+') {
echo "OK";
}
回答by Lance Rushing
To see of the curent time is greater or equal to 14:08:10 do this:
要查看当前时间是否大于或等于 14:08:10,请执行以下操作:
if (time() >= strtotime("14:08:10")) {
echo "ok";
}
Depending on your input sources, make sure to account for timezone.
根据您的输入源,确保考虑时区。
See PHP time()and PHP strtotime()
回答by Gaurav Gupta
Simple way to compare time is :
比较时间的简单方法是:
$time = date('H:i:s',strtotime("11 PM"));
if($time < date('H:i:s')){
// your code
}