Jump to content

Putting date into database as month name and year.


davidcriniti

Recommended Posts

Hi,

 

I've got a date picker on a form which puts data into a database in the YYYY-MM-DD format. Just wondering how I could also put the name of the month (extracted from that) as well as just the year into separate columns. Ie: To use the field race_date from the form to also fill the 'race_month' and 'race_year' columns in the database.

 

This code obviously only fills the 'race_date' column so far:


global $_POST;

$race_date = $_POST["race_date"] ;


....



$query = "INSERT INTO 10k_races (race_date, race_month, race_year)" . "VALUES ( '$race_date', '$race_month',  '$race_year')";

 

also put the name of the month (extracted from that) as well as just the year into separate columns

 

^^^ That would result in duplicate/redundant information stored in your table and would be a bad design.

 

To get the year and the month name from your race_date column any time you need those values, you would simply use the mysql YEAR() and MONTHNAME() functions in your queries.

Archived

This topic is now archived and is closed to further replies.

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