If you want to just do it in MySQL, you can use the WEEK()
function:
SELECT WEEK(date) as week, YEAR(date) as year FROM mytable
Note, you may want to specify a mode number for WEEK()
to tell MySQL which day of the week to start on. E.g. some servers may be configured to start weeks on Mondays, whereas other may start on Sundays.
SELECT WEEK(date, 0) as week ... /* explicitly start weeks on Sundays */
There are also other week number functions for MySQL that you might want to check out, such as YEARWEEK()
, which returns the year + week number (e.g. 201012):
http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_week