PHP Checking if timestamp is less than 30 minutes old

PHPtimestamp

I'm getting a list of items from my database, each has a CURRENT_TIMESTAMP which i have changed into 'x minutes ago' with the help of timeago. So that's working fine. But the problem is i also want a "NEW" banner on items which are less than 30 minutes old. How can i take the generated timestamp (for example: 2012-07-18 21:11:12) and say if it's less than 30 minutes from the current time, then echo the "NEW" banner on that item.

Best Answer

Use strtotime("-30 minutes") and then see if your row's timestamp is greater than that.

Example:

<?php
    if(strtotime($mysql_timestamp) > strtotime("-30 minutes")) {
        $this_is_new = true;
    }
?>

I'm using strtotime() twice here to get unix timestamps for your mysql date, and then again to get what the timestamp was 30 minutes ago. If the timestamp from 30 mins ago is greater than the timestamp of the mysql record, then it must have been created more than 30 minutes go.

Related Topic