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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.