Selection the month for date entry

Discussion in 'PHP' started by adamjblakey, Nov 15, 2008.

  1. #1
    Hi,

    I have a DB with the date for each record in format yyyy-mm-dd i want to run a select statement which only selects the month.

    E.g. Something like this:
    SELECT * FROM `table` WHERE date(month) = '$month'
    Code (markup):
    Cheers,
    Adam
     
    adamjblakey, Nov 15, 2008 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    You didn't specified nor field type, nor database engine.
    If it is Mysql and field is datetime, you can use a SQL:

    SELECT * FROM `table` WHERE MONTH(yourdatefield) = '$month'
    Code (markup):
    where $month is a month number.

    Also to speed up this select, I would recommend to add an index on this datetime field.
     
    wmtips, Nov 15, 2008 IP