ilhan
ilhan

Reputation: 8963

Combining the date in order to insert it to the database

I need to combine day, month and year in PHP in order to insert it to MySQL database. How I'll combine the day, the month and the year? I'm choosing the date from a drop down menu.

Upvotes: 2

Views: 2565

Answers (2)

Majid Fouladpour
Majid Fouladpour

Reputation: 30252

MySQL expects the date in this format: yyyy-mm-dd, so if you are using seprate drop downs for day, month and year you can cocatenate them like so:

$date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'];

If your form's method is 'get' instead of 'post', then substitute $_POST with $_GET in the code. Also make sure the value is set correctly for days 1 to 9 as 01 to 09 and you use a two digit value - the same for month. I.e.

<select name="month">
   <option value="01">1</option>
   ...
</select>

Upvotes: 2

Rob Farley
Rob Farley

Reputation: 15849

If the drop-downs have numeric values, you can just use ordinary date adding functions.

Upvotes: 1

Related Questions