I'm trying to get a recordset of all records with a release date newer than 60 days. I first create a date variable of 60 days ago. Then use it in my MySQL query statement shown below. However, when I display the results, I get ALL the records, regardless of date. My dates are stored in DATE formatted data in MySQL.
$TwoMonthsAgo = date("Y-m-d", mktime(0,0,0, date(m), date(d)-60,date(Y)));
SELECT *
FROM music
WHERE ReleaseDate > $TwoMonthsAgo
I've been reading up on much of the date functions in PHP, but I'm not sure what the best way to go about this is. Am I at least on the right track?
Problems with dates in MySQL statement
Moderator: General Moderators
Re: Problems with dates in MySQL statement
I figured it out. This seems to work. If anyone knows of a better way or reason this is not a good method, please post.Swede78 wrote: $TwoMonthsAgo = date("Y-m-d", mktime(0,0,0, date(m), date(d)-60,date(Y)));
SELECT *
FROM music
WHERE ReleaseDate > $TwoMonthsAgo
$TwoMonthsAgo = mktime(0,0,0, date(m), date(d)-60,date(Y));
SELECT *
FROM music
WHERE UNIX_TIMESTAMP(ReleaseDate) > $TwoMonthsAgo
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You could do the comparison using SQL:
http://www.mysql.com/doc/en/Date_and_ti ... tions.html
Mac
Code: Select all
SELECT * FROM music WHERE ReleaseDate >= DATE_SUB(CURDATE(), INTERVAL 2 MONTH)Mac