php 如何比较两个24小时制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8697361/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:18:01  来源:igfitidea点击:

How to compare two 24 hour time

php

提问by Ramprasad

I have two 24-hour time values and want to compare them using PHP.

我有两个 24 小时时间值,想使用 PHP 比较它们。

I have tried the following:

我尝试了以下方法:

$time="00:05:00"; //5 minutes

if($time1<='00:03:00')
{
 //do some work
}
else
{
 //do something
}

Is this the correct way to compare 2 time values using PHP?

这是使用 PHP 比较 2 个时间值的正确方法吗?

回答by rsaxvc

Use the built-in function strtotime():

使用内置函数strtotime()

$time="00:05:00"; //5 minutes
if(strtotime($time)<=strtotime('00:03:00')) {
 //do some work
} else {
 //do something
}

回答by Niet the Dark Absol

Yes, that is correct. You don't need to convert to an integer because 24-hour format is such that the string value is in the exact same order as the numeric.

对,那是正确的。您不需要转换为整数,因为 24 小时格式的字符串值与数字的顺序完全相同。

回答by Sudhir Bastakoti

You could use, php's strtotime

你可以使用 php 的strtotime