Database DATE compare

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
gls2ro
Forum Newbie
Posts: 1
Joined: Mon Apr 05, 2004 9:26 am

Database DATE compare

Post by gls2ro »

Hello!
I have a little problem.
I have a date and a name field in my database's table and I want to list all the names that have been inserted in the last x days.
Example
Name1 2004-04-01
Name2 2004-03-02
Name 3 2003-11-03

And I want for example to list all the names that have been inserted in the last 40 days:
Name1
Name2

Thank You!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you could try this

Code: Select all

// what date was it 40 days ago in format yyyy-mm-dd
$fourtyDaysAgo = date('Y-m-d',strtotime(date('Y-m-d').' -40 days'));

// build query
$sql = "SELECT * FROM users WHERE date >= '$fourtyDaysAgo'";
Mark
Post Reply