简体   繁体   English

如何比较php中的24小时时间格式?

[英]How to compare 24hrs time format in php?

I have time in 24hrs date format.我有 24 小时日期格式的时间。 For example例如

$after = 9;
$before = 17;

and my enter time is我的进入时间是

$enterTime = 15;

I want to know my enter time $enterTime is outside of $after and $before time.我想知道我的输入时间$enterTime$after$before时间之外。 For this, I am using this code.为此,我正在使用此代码。

if(strtotime($enterTime) > strtotime($before) && strtotime($enterTime) < strtotime($after))
{
    echo $enterTime;
    } else {
        echo"jghj";
}

You can't compare like this你不能这样比较

if(strtotime($enterTime) > strtotime($before) && strtotime($enterTime) < 
strtotime($after))
{
    echo $enterTime;
} else {
    echo"jghj";
}

because, strtotime() function takes parameters Parameters ¶ time: A date/time string.因为, strtotime() 函数接受参数参数 ¶ time:日期/时间字符串。 Valid formats are explained in Date and Time Formats.日期和时间格式中解释了有效格式。

now: The timestamp which is used as a base for the calculation of relative dates.现在:用作计算相对日期的基础的时间戳。 but you are passing integer value in strtotime().但是您在 strtotime() 中传递整数值。 which is invalid value.这是无效值。

you can compare like so你可以这样比较

$beforeTime ="14:08:10";
$afterTime ="15:10:00";
if (strtotime(afterTime ) >= strtotime($beforeTime)) {
  echo "ok";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM