PHP code help - date related

Soldato
Joined
18 Oct 2002
Posts
7,639
Location
Sutton Coldfield, Birmingham
I have a list of items in a database and they all have a date when they were added.

The date field in the database is a timestamp - YYYY-MM-DD 00:00:00

I want to display the items on a page and have them listed as 'NEW' if they are less than 7 days old, how would I go about doing this with mktime, etc?

I've read the php manual on mktime and it confuses the hell out of me haha.
 
If you have PHP5.3+ you can use something like this:

PHP:
<?php
$dateDB = new DateTime('2010-08-22 16:26');
$dateNow = new DateTime('now');

$interval = $dateDB->diff($dateNow);

echo sprintf( "Product is: %s.", ($interval->days <= 7 ? "New" : "Old") );
?>
Based on this.
 
Last edited:
I think strtotime should cope.

PHP:
$mydate = "2001-10-10 01:00:00";
$f_date = strtotime($mydate);
$threshold = time() - 604800;
if ( $f_date > $threshold ) echo "NEW";
 
Back
Top Bottom