Jump to content

[SOLVED] Search by date


gijew

Recommended Posts

I was having trouble performing queries using unix timestamps (which is my preferred method - just getting that out of the way) so I felt like going old school and using the datetime field type in MySQL for this project.

 

My problem is that I want to query MySQL to return a specific date (month / year).

 

Here's what I have:

 

:: Example data

2008-04-12 23:27:21

2008-04-13 00:12:10

2008-03-12 00:49:28

2007-12-25 13:14:15

 

:: Searching for

04-2008

 

It should return record 1 & record 2.

 

I could post my query but I already know I'm way off so I'm not going to embarass myself further :)

 

How would one go about doing this? I tried returning something with DATE_FORMAT (created_on, '%M %Y) and even a few variations of the DATE function inside of MySQL but it's returning nothing as I'm obviously missing something here.

 

Suggestions?

Link to comment
https://forums.phpfreaks.com/topic/100983-solved-search-by-date/
Share on other sites

The quickest query would be to just test the month and year of the date against the month and year you are searching for, rather than to run the date in each row through the format function -

 

:: Searching for

04-2008

 

WHERE YEAR(created_on) = 2008 AND MONTH(created_on) = 4

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.