PHP日期时间功能:过期内容

时间:2014-10-01 10:47:07

标签: php

如何使用两天后添加日期的内容到期。 从mysql获取数据后

    //looop starts
    {
       $db_Date = $rows['created_Date']; //output 2014-01-10 12:08:56
      /* Now i have to expire the content after 2 days from the added date and time i.e i want to expire by 2014-03-10 12:08:56*/
//    I tried like this
        $date = new DateTime($rows['created_Date'];);
        $now = new DateTime();  

        if($date < $now) {
            echo 'date is in the past';
        } // this is not working as 

       <div>My Content</div>
    }

2 个答案:

答案 0 :(得分:0)

您可以使用DateTime::add()

$date = new DateTime($rows['created_Date']);
$date->add(new DateTimeInterval('P2D'));

您可以使用DateTime::modify()

$date = new DateTime($rows['created_Date']);
$date->modify('+2 days');

(要完成此答案并考虑到h2ooooooo的答案),您可以直接在SQL中执行此操作:

SELECT data FROM table WHERE id = 123 AND date >= DATE_SUB(NOW(), INTERVAL 2 DAY)

答案 1 :(得分:0)

您可以使用modify方法。

 {
       $db_Date = $rows['created_Date']; //output 2014-01-10 12:08:56
      /* Now i have to expire the content after 2 days from the added date and time i.e i want to expire by 2014-03-10 12:08:56*/
//    I tried like this
        $date = new DateTime($rows['created_Date']);
        $date = $date->modify('+2 days');

        $now = new DateTime();  

        if($date < $now) {
            echo 'date is in the past';
        } // this is not working as 

       <div>My Content</div>
    }