Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing datetime value to today's date to get number of days since. 1

Status
Not open for further replies.

superslurpee

Programmer
May 15, 2002
108
Hi,

I have a datetime in a mysql db and want to compare it against right now. Basically if the date in the db is less than 7 days old, show a 'new' image. How do I subtract this datetime value from the current one to get the difference?

Thanks!!!

Darth Slurpee
 
Code:
$datetime = "2006-12-01 23:37:00"; //see [URL unfurl="true"]http://www.gnu.org/software/tar/manual/html_node/tar_109.html#SEC109[/URL]

if (time()-strtotime($datetime) > (7*24*60*60)){
 //show new image
}else{
 //show old image
}
 
Hi!

Thanks a lot. Just one minor correction in case someone else needs help with this in the future and looks up this post. To show the new image if the date is less than 7 days, change the greater than sign to less than.

Cheers!



Darth Slurpee
 
my apologies, i thought you wanted the image "timing out" and thus an older stamp would mean a newer image. i didn't read your post thoroughly enough.
 
Why not do this in your SQL:

Code:
DATEDIFF(`written`,CURDATE()) as daysold

Then, when looping out your products:
if ($row['daysold'] <=7) {
echo "New!";
}

I think this would execute quicker than doing the datediff in php?

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top