首页 > 解决方案 > 如何获得空日期时间戳和其他确定的持续时间?

问题描述

我正在编码以计算空时间戳日期和另一个非空日期之间的持续时间,请帮助我。

我想计算此持续时间以设置确认代码在论坛参与者回来太晚或超过 2 天没有回来时过期作为最大延迟

此代码工作正常,除非返回日期为空:

$date_participation= strtotime($row['date_participation']);
$date_come_back= strtotime($row['date_come_back']);

$expiration_delay = 2;

$days = $expiration_delay * 86400;
echo "<br> duration in seconds is: " . $date_come_back- 
$date_participation;

if (($date_come_back- $date_participation) >= $days){
    $sql_set_expired = "UPDATE `winners` SET `confirmation_status` = 'expired' WHERE id_recharge_winner ='$i'";
    $set_expired_result = mysqli_query($conn, $sql_set_expired);
       }

     the `$i` is the participant's row in table winners

标签: phpdatetimestampdelayduration

解决方案


这就是我在不担心是否有空日期的情况下导致我的问题的方式:我创建了一个函数 Difference_between_timestampdate() ,而不是我指定的一天,例如到期持续时间。它工作正常,感谢帮助我的人。

 function Difference_between_timestampdate($first_date, $last_date){

 $first_date = strtotime($first_date );
 $last_date = strtotime($last_date);

  $days = $expiration_delay * 82800;

 if(empty($last_date)){
 $duration = time() - $first_date;
  }else{
 $duration = $last_date - $first_date;
 }
 return $duration;
 }


$date_participation = $row['date_participation'];
$date_come_back = $row['date_come_back'];

$expiration_delay = 1; //One day
if ((Difference_between_timestampdate($date_participation, $date_come_back)/82800 ) 
>= $expiration_delay){echo 'Expired duration';}else{echo 'Valid duration';}

推荐阅读