How to create a table with a row for each day of a given year?
I need to develop a calendar which tells you if a given day is of type A, B, C or D. So basically, users requests info for a given day, and the website tells them if that day is of type A, B, C or D. So I will create a PHP function that checks for the given day in the database, and returns its type.
Is it possible (and if it is, is it a good practice) to programatically create the mysql开发者_如何学Go table with a row for each day of the year, with a DATE type to identify each day? How can I do this?
EDIT: In the end, I used @nic's idea but instead I coded it in PHP, and created a reusable function with it. The function expects a string parameter, containing the year in YYYY format, and creates a table with a row for each day of the year cointaining a DATE field, and several text fields for day, month, year and weekday.
You can find it here
I would say yes, it's a good practise. You could also have a number table in your database. It can be handy in certain situations.
Check Ronnis' answer for how to create a Calendar table, like the one you want. By number table, I mean exactly the numbers
table he creates.
You could store the dates for many years, like a hundred years. This would mean a table with only about 36500 rows.
In the end, I used @nic's idea but instead I coded it in PHP, and created a reusable function with it. The function expects a string parameter, containing the year in YYYY format, and creates a table with a row for each day of the year cointaining a DATE field, and several string fields for day, month, year and weekday.
You can find it here
精彩评论