php 如何在PHP中比较两个时间

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

How to compare two time in PHP

phpdatetime

提问by Rajasekar

I had two times in the format like 7:30:00 and 22:30:00 stored in the variables $resttimefromand $resttimetorespectively.

我有两次分别以 7:30:00 和 22:30:00 的格式存储在变量$resttimefrom和 中$resttimeto

I want to check whether the current time is between these two values. I am checking this with the code

我想检查当前时间是否在这两个值之间。我正在用代码检查这个

$time = date("G:i:s");
if ($time > $resttimefrom and $time < $resttimeto ){
    $stat = "open";
} else {
    $stat = "close";
} 

But I am always getting the $stat as Close. What may cause that?

但我总是得到 $stat 作为关闭。什么可能导致这种情况?

采纳答案by Rajasekar

As Col. Shrapnel Said i am doing by converting all the time in to seconds and then compare it with current time's total seconds

正如 Shrapnel 上校所说,我通过将所有时间转换为秒,然后将其与当前时间的总秒数进行比较

回答by Alfwed

A simple yet smart way to do this is to remove the ':' from your dates.

一个简单而聪明的方法是从您的日期中删除“:”。

$resttimefrom = 73000;
$resttimeto = 223000;

$currentTime = (int) date('Gis');

if ($currentTime > $resttimefrom && $currentTime < $resttimeto )
{
    $stat="open";
}
else
{
    $stat="close";
} 

回答by Zuber Surya

you can try using strtotime

你可以尝试使用 strtotime

$st_time    =   strtotime($resttimefrom);
$end_time   =   strtotime($resttimeto);
$cur_time   =   strtotime(now);

then check

然后检查

if($st_time < $cur_time && $end_time > $cur_time)
{
   echo "WE ARE CLOSE NOW !!";
}
else{
   echo "WE ARE OPEN  NOW !!";
}

i hope this may help you..

我希望这可以帮助你..

回答by quantumSoup

$today = date("m-d-y ");
$now = date("m-d-y G:i:s");

if (strtotime($today . $resttimefrom) < $now && $now > strtotime($today . $resttimeto)) {
    $stat = 'open';
else
    $stat = 'close

回答by Sebs

Try reformatting them into something that you can compare like that. For example, numbers:

尝试将它们重新格式化为您可以进行比较的内容。例如,数字:

$resttimefrom = mktime(7,30,0);
$resttimeto = mktime(22,30,0);

$time = mktime(date('H'),date('i'),date('s'));

回答by user395702

Just convert your dates to a Unix Timestamp, compare them, you have your results! It might look something like this:

只需将您的日期转换为 Unix 时间戳,比较它们,您就会得到结果!它可能看起来像这样:

$time =date("G:i:s");

$time1 = strtotime($time); 
$resttimefrom1 = strtotime($resttimefrom );
$resttimeto1 = strtotime($resttimeto );
if ($time1 >$resttimefrom  and $time1 <$resttimeto)
{
    $stat="open";
}
else
{
    $stat="close";
} 

回答by Gordon

You are comparing strings.
Convert the Time Strings to timestamps with strtotime().
Then compare against time().

您正在比较字符串。
将时间字符串转换为时间戳strtotime()
然后比较time()

回答by Jonathon Bolster

The datefunction returns a string, so the comparison you're making would be a string comparison - so 7:30 would be more than 22:30

date函数返回一个字符串,因此您进行的比较将是字符串比较 - 所以 7:30 将超过 22:30

It would be much better to use mktime, which will return a Unix timestamp value (integer) so it would make for a better comparison

最好使用mktime,它将返回一个 Unix 时间戳值(整数),以便进行更好的比较

$currentTime = mktime();
$resttimefrom = mktime(hour,min,second);

http://php.net/mktime

http://php.net/mktime

回答by emurano

The trick to manipulating and comparing dates and times in PHP is to store date/time values in an integer variable and to use the mktime(), date() and strtotime() functions. The integer repesentation of a date/time is the number of seconds since midnight, 1970-Jan-1, which is referred to as the 'epoch'. Once your date/time is in integer form you'll be able to efficiently compare it to other dates that are also in integer form.

在 PHP 中操作和比较日期和时间的技巧是将日期/时间值存储在一个整数变量中,并使用 mktime()、date() 和 strtotime() 函数。日期/时间的整数表示是自 1970 年 1 月 1 日午夜以来的秒数,称为“纪元”。一旦您的日期/时间采用整数形式,您就可以有效地将其与其他同样采用整数形式的日期进行比较。

Of course since you'll most likely be receiving date/time values from page requests and database select queries you'll need to convert your date/time string into an integer before you can do any comparison or arithmetic.

当然,由于您很可能会从页面请求和数据库选择查询中接收日期/时间值,因此您需要将日期/时间字符串转换为整数,然后才能进行任何比较或算术。

Assuming you are sure that the $resttimefrom and $resttimeto variables contain properly formatted time you can use the strtotime() function to convert your string time into an integer. strtotime() takes a string that is formatted as a date and converts it to the number of seconds since epoch.

假设您确定 $resttimefrom 和 $resttimeto 变量包含格式正确的时间,您可以使用 strtotime() 函数将字符串时间转换为整数。strtotime() 接受一个格式化为日期的字符串并将其转换为自纪元以来的秒数。

$time_from = strtotime($resttimefrom);
$time_to = strtotime($resttimeto);

Side note: strtotime() always returns a full date in integer form. If your string doesn't have a date, only a time, strtotime() return today's date along with the time you gave in the string. This is not important to you, though, because the two dates returned by strtotime() will have the same date and comparing the two variables will have the desired effect of comparing the two times as the dates cancel each other out.

旁注:strtotime() 总是以整数形式返回一个完整的日期。如果您的字符串没有日期,只有时间,则 strtotime() 将返回今天的日期以及您在字符串中指定的时间。不过,这对您来说并不重要,因为 strtotime() 返回的两个日期将具有相同的日期,并且比较两个变量将具有比较两个时间的预期效果,因为日期相互抵消。

When you compare the two integers keep in mind that the earlier the date/time is, the smaller its integer value will be. So if you want to see if $time_from is earlier than $time_to, you would have this:

当您比较两个整数时,请记住,日期/时间越早,其整数值就越小。因此,如果您想查看 $time_from 是否早于 $time_to,则可以使用以下命令:

if ($time_from < $time_to)
{
    // $time_from is ealier than $time_to
}

Now to compare a date/time with the current system date/time, just use mktime() with no parameters to represent the current date/time:

现在要将日期/时间与当前系统日期/时间进行比较,只需使用不带参数的 mktime() 来表示当前日期/时间:

if ($time_from < mktime())
{
    // $time_from is in the past
}

回答by Sachin R

$firstTime = '1:07';
$secondTime = '3:01';

list($firstMinutes, $firstSeconds) = explode(':', $firstTime);
list($secondMinutes, $secondSeconds) = explode(':', $secondTime);

$firstSeconds += ($firstMinutes * 60);
$secondSeconds += ($secondMinutes * 60);
$difference = $secondSeconds - $firstSeconds;

回答by Raj Kumar

$Time1 = date_parse($time);
$seconds1 = $Time1['hour'] * 3600 + $Time1['minute'] * 60 + $Time1['second'];

$Time2 = date_parse($current_time);
$seconds2 = Time2['hour'] * 3600 + Time2['minute'] * 60 + Time2['second'];

$actula_time = $seconds1 - $seconds2;

echo floor($actula_time / 3600) .":". floor(($actula_time / 60)%60) .":".  $actula_time%60;