php:如何检查mysql数据是否过期

时间:2015-02-11 12:36:04

标签: php mysql

我有一个mysql格式的数据,例如:" 2015-02-09 23:12:33" 有没有办法检查这些数据是否是24小时或更长时间?

谢谢

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT
 IF(yourDate<DATE_SUB(NOW(), INTERVAL 1 DAY), 1, 0) AS expired

或者这个:

SELECT
 IF(yourDate<DATE_SUB(NOW(), 24 HOUR), 1, 0) AS expired

答案 1 :(得分:0)

使用MySql Date函数

SELECT DATEDIFF(dateColumn, NOW()) as "diff" FROM tableName HAVING diff < 1;

未经过测试


在PHP中(使用PDO):

$query = $db->prepare('SELECT `dateColumn` FROM `tableName`');
$query->execute();
$result = $sth->fetchAll(PDO::FETCH_CLASS);

foreach ($result as $date) {
    $time = strtotime($date->dateColumn);

    if (time() - $time < 60 * 60 * 24) {
        // It's less than 24h ago;
    } else {
        // It's more than 24h ago;
    }
}

只需为PDO设置正确的参数即可将行作为对象获取。