需要知道给定日期是否已过期

时间:2012-10-25 17:39:51

标签: php datetime

我需要知道如何在php中了解某个日期是否已过期?我需要在包管理器中显示相同的状态,我需要知道包是活动的还是已过期的。我在mysql表中有两个名为start_date和expiry_date的字段。

我需要知道如何从star_date中减去expiry_date并获取值,然后检查expiry_date是否已过期或尚未过期。

请帮帮我。

2 个答案:

答案 0 :(得分:3)

好的,我会做一些假设,以便我的回答更清晰。

如果你的expiry_date是包过期的日期,你可以使用php检查它是否是将来的日期,或者不是这样。

$exp_date = strtotime($expiry_date);

if(time() > $exp_date) // your package has expired.

答案 1 :(得分:1)

在MySQL中尝试DATEDIFF

SELECT DATEDIFF(expiry_date,start_date) AS difference FROM packages...

或者在PHP中,您可以遍历答案并选择以下内容:

 $curr_date = date('Y-m-d'); // or your date format

// loop

if ($row['start_date'] <= $curr_date && $row['expiry_date'] > $curr_date){
  // post is active
}
elseif ($row['expiry_date'] > $curr_date)
{
   // post expired
}
相关问题