[英]PHP - Comparing date, checking if within last hour
I'm attempting to see if a time is within the last hour.我正在尝试查看时间是否在最后一小时内。
$unit_date = date($unit['last_accessed'], 'Y-m-d H:i:s');
$last_hour = date('Y-m-d H:i:s', strtotime('-1 hour'));
if($unit_date >= $last_hour ){
$connect = "<i class='fa fa-circle 2x' style='color:#2ECC71;'></i>";
} else {
$connect = '';
}
As you can see I'm putting $unit['last_accessed'] into the correct format then comparing to an hour ago.如您所见,我将 $unit['last_accessed'] 放入正确的格式,然后与一小时前进行比较。 If the time is within the last hour $connect is a font awesome circle (colored green), if it's not within the last hour it's empty, right now it's saying nothing is within the last hour which is false.如果时间在最后一小时内 $connect 是一个字体很棒的圆圈(绿色),如果不是在最后一小时内它是空的,现在它说在最后一小时内什么都没有,这是错误的。
if(time() - strtotime($unit['last_accessed']) < 3601){
or 要么
if(time() - strtotime($unit['last_accessed']) > 3599){
time() and strtotime() both use the same time base in seconds time()和strtotime()都以秒为单位使用相同的时基
if $unit['last_accessed'] is already an integer time variable then do not use strtotime(). 如果$ unit ['last_accessed']已经是整数时间变量,那么不要使用strtotime()。
First of all, you're using date function in a wrong way. 首先,您正在以错误的方式使用日期功能。 From PHP's manual: 从PHP的手册:
date ( string $format [, int $timestamp = time() ] )
You must provide as first argument the $format string and the $timestamp as the second. 您必须提供$ format字符串作为第一个参数,并将$ timestamp作为第二个参数。 You can check if the time is whitin the last hour without transform the Unix Timestamp to a another timestamp string. 您可以检查时间是否在最后一小时,而不将Unix时间戳转换为另一个时间戳字符串。
$last_hour = time() - 60*60; //last hour timestamp
if($unit['last_accessed'] >= $last_hour){
$connect = "<i class='fa fa-circle 2x' style='color:#2ECC71;'></i>";
}else{
$connect = '';
}
As you can see, i didnt made any transformation to the timestamp, as i'm not using the timestring in anywhere. 正如你所看到的,我没有对时间戳进行任何转换,因为我没有在任何地方使用时间轴。 You should learn a little more about operations with unix timestamp's or about php time functions. 您应该了解有关使用unix时间戳或php时间函数的操作的更多信息。
References: http://php.net/manual/en/function.date.php 参考文献: http : //php.net/manual/en/function.date.php
DateTime solution日期时间解决方案
$firstDate = new \DateTime('2019-10-16 07:00:00');
$secondDate = new \DateTime('2019-10-17 07:00:00');
$diff = $firstDate->diff($secondDate);
$diffHours = $diff->h;
// diff in hours (don't worry about different years, it's taken into account)
$diffInHours = $diffHours + ($diff->days * 24);
// more than 1 hour diff
var_dump($diffInHours >= 1);
Origin similar answer: Calculate number of hours between 2 dates in PHP来源类似的答案: Calculate number of hours between 2 dates in PHP
You've got an error in your code. 你的代码中有错误。 In one place you call date
like this: 在一个地方,你date
这样称呼date
:
date( format , time ) 日期( 格式 , 时间 )
and in another place, like this: 在另一个地方,像这样:
date( time , format ) 日期( 时间 , 格式 )
Check the PHP documentation for date
and figure out which one is correct. 检查PHP文档的date
,找出哪一个是正确的。 Otherwise your code looks logically correct, if perhaps needlessly complex. 否则,您的代码看起来在逻辑上是正确的,如果可能不必要的复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.