I want to add a link "renew post" after X amount of days after the user has posted their post on the website which will rearrange the post to the top (based on date that is retrieved from the database of course). the timestamp format on my database date row is 2014-02-12 15:06:44

    $date = date('y-M-d l H:i a', strtotime($row['date']));
    $days = 30; //what to put here?
    if ($date > $days){
       echo '<a href="$clicked">link here</a>';

       if ($clicked === true){
          mysql_query("UPADTE `posts` SET `date` = now()");
       }
    }

the link should allow them to UPDATE the timestamp which i know i did wrong as i didn't know how to call the link..

i am not sure what $days should be set to.. any help is appreciated!

有帮助吗?

解决方案

Use a DateInterval...

$date = new DateTime($row['date']);
$now = new DateTime();
$diff = $date->diff($now);
$daysOld = $diff->days;

Or in a nice, tidy one-liner...

$days = 30;
if ((new DateTime($row['date']))->diff(new DateTime())->days > $days) {
    // etc
}

其他提示

$thirty_days_ago = time()-24*60*60*30;
if (strtotime($row['date'])<$thirty_days_ago){

There is another way to calculate date before 30 days from current date with php. we can use day,month and year here.For future dates calculation it will be +

$currentday = "2014-02-28";
$postdate = date($currentday,strtotime("-30 day")); //date before 30 days
$postdate = date($currentday,strtotime("+1 day")); //date after one day
$postdate = date($currentday,strtotime("+1 week")); //date after one week
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top