I have an html form from which I am trying to insert the data into a MySQL db. I have heard that the date needs to be in a specific format for inserting into the table, I believe it was YearDayMonth (20070612) Is this true? Because of that, I broke the date into 3 fields with drop down menus to make sure that I got the information in the correct format. Maybe there was an easier way to script this, but this is what I came up with:

<p>Date: <select name="date_month">
	<option value="jan">January</option>
	<option value="feb">Febuary</option>
	<option value="mar">March</option>
	<option value="apr">April</option>
	<option value="may">May</option>
	<option value="jun">June</option>
	<option value="jul">July</option>
	<option value="aug">August</option>
	<option value="sep">September</option>
	<option value="oct">October</option>
	<option value="nov">November</option>
	<option value="dec">December</option>
	</select>
	<select name="date_day">
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
		<option value="13">13</option>
		<option value="14">14</option>
		<option value="15">15</option>
		<option value="16">16</option>
		<option value="17">17</option>
		<option value="18">18</option>
		<option value="19">19</option>
		<option value="20">20</option>
		<option value="21">21</option>
		<option value="22">22</option>
		<option value="23">23</option>
		<option value="24">24</option>
		<option value="25">25</option>
		<option value="26">26</option>
		<option value="27">27</option>
		<option value="28">28</option>
		<option value="29">29</option>
		<option value="30">30</option>
		<option value="31">31</option>
		
	</select>
	<select name="date_year">
		<option value="07">2007</option>
		<option value="08">2008</option>
		<option value="09">2009</option>
	</select>

My question is, what is the script to change the three fields into one variable to be added to the table? Do I just do something like $TIMESTAMP=(date_year + date_day + date_month) Another question that I had, does timstamp automatically make it the current date for when it is being inserted, or can I have it be user set like above?

Dani AI

Generated

A quick follow-up to what started and suggested: keeping three select boxes is fine, but validate and format the pieces on the server before inserting. Two common mistakes are accepting two‑digit years (like "07") without converting them, and inserting unvalidated dates (Feb 29 on a non‑leap year).

Validate and build a safe MySQL DATE string server‑side. Example (validation + zero padding):

$y = (int) $_POST['date_year'];
$m = (int) $_POST['date_month'];
$d = (int) $_POST['date_day'];

if (checkdate($m, $d, $y)) {
    $mysqlDate = sprintf('%04d-%02d-%02d', $y, $m, $d);
} else {
    // handle invalid date
}

See PHP docs for checkdate and sprintf for details: checkdate and sprintf. If you prefer parsing a single user string, strtotime + date can normalize many formats: strtotime.

Notes about MySQL types and TIMESTAMP behavior: DATETIME stores whatever you give it (no timezone conversion). TIMESTAMP stores values in UTC internally and can auto‑set or auto‑update when the column uses DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP. Use TIMESTAMP when you want automatic current time and timezone handling; use DATE/DATETIME when you need to store a user‑entered date unchanged. MySQL docs: Date and Time types and .

Finally: always use prepared statements (PDO or MySQLi) when inserting the final value, convert any two‑digit years to four digits first, and run server‑side checks rather than relying on client HTML alone. This avoids bad data and keeps your inserts predictable.

Recommended Answers

All 4 Replies

Try using different values for your select options. It makes it much easier to assemble a date in the proper format, which is yyyy-mm-dd.

<select name="date_year">
<option value="2007">2007</option>

<select name="date_month">
<option value="01">January</option>

<select name="date_day">
<option value="01">1</option>

You can then assemble the date string in the proper format on the receiving end.

$theDate = $_POST."-".$_POST."-".$_POST;

Thanks TopDogger. I'll try and make those changes that you suggested.

I know I marked this as solved already, but I just noticed something. TopDogger recommended to use: $theDate = $_POST['date_year']."-".$_POST['date_month']."-".$_POST['date_day']; I just noticed that there are some '.' (dot) in the line. What is the reason for putting the dot there?

You use dots to concatinate string values in PHP. The dots do not appear in the final value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.