1

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!

halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

0

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
}
Phil
  • 157,677
  • 23
  • 242
  • 245
  • that's weird, once i uploaded it to my host online it tells me " syntax error, unexpected T_OBJECT_OPERATOR" but on localhost it works.. how do i define the dateinterval? –  Feb 13 '14 at 00:31
  • @Bamba Sounds like a difference in PHP versions. Try the top, more verbose snippet. What version is your host? – Phil Feb 13 '14 at 00:59
  • 5.2.x, im using a free hosting service to see how it looks live on my phone and all (localhost is limiting to only my pc) but yeah i guess that is the problem.. have to get a better host –  Feb 13 '14 at 01:02
  • Yikes. 5.2 isn't even supported any more. If your phone is on the same network as your local server (via wifi), you should be able to hit it up by its IP – Phil Feb 13 '14 at 02:03
0
$thirty_days_ago = time()-24*60*60*30;
if (strtotime($row['date'])<$thirty_days_ago){
symcbean
  • 47,736
  • 6
  • 59
  • 94
0

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
Geo V L
  • 866
  • 8
  • 25