PHP检查日期是否为一年

时间:2014-09-10 21:05:03

标签: php date

我正在尝试检查MySQL数据库中的日期是否比当前日期(脚本运行的那一天)早一年。

我不确定如何正确检查这个。这是我到目前为止所做的:

if ($comparableDate is 1 year older than $todaysDate) {
        // delete this row from the database
}

PHP:

<?php
// Get Database parameters
require_once 'settings.php';

date_default_timezone_set('America/New_York');

// Attempt to connect to Database
try {
    $con = new PDO($db_dsn, $db_username, $db_password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

$applications = $con->query("SELECT * FROM Applications")->fetchAll(PDO::FETCH_OBJ);

$todaysDate = getdate();
$todaysDate = $todaysDate[0];

foreach ($applications as $application) {
    $date = $application->dateSubmitted;
    $comparableDate = strtotime($date);

    if ($comparableDate is 1 year older than $todaysDate) {
        // delete this row from the database
    }
}
?>

有人可以帮我解决这个问题吗?感谢。

1 个答案:

答案 0 :(得分:3)

使用strtotime,您可以PHP manual说,&#34;将任何英文文本日期时间描述解析为Unix时间戳&#34;。

所以,你的代码可能是这样的:

...
foreach ($applications as $application) {
    $comparableDate = strtotime($application->dateSubmitted);

    if ($comparableDate < strtotime('-1 year')) {
        // delete this row from the database
    }
}