Search code examples
phpmysqlsqldateselect-menu

Month and Year with PHP and MySQL?


I need to add a column to my MySQL database for the month and year. For example, when inserting an entry, it can either be December 2010, April 2009, May 2011, etc.

Can someone provide me with some SQL code that I can use to insert this into my database? I'm confused as to whether I should store it as 2011-07-30, a Unix timestamp, etc.

I also need to do the following:

  • Drop down for the month/year (going back 5 years and in advance 5)
  • Display the selected month/year when editing entries
  • Be able to get results when doing searches like December 2010

If you could also provide examples of how to do the items above that would be excellent.


Solution

  • for month and year you can add following query in your database string...

    select to_char(date_column_name, 'MONTH YYYY') from table_name
    

    click here for more information

    for drop down you can use following condition over there

    where to_number(to_char(date_column_name,'YYYY')) 
            >= to_number(to_char(curdate,'YYYY') ) -5
    

    click here for more information

    •Be able to get results when doing searches like December 2010
    

    for this last option you have to pass this text box value to database and want to put this value in where condition like below-

    select * from table_name 
    where to_char(date_column_name,'MONTH YYYY') = front_end_object_name