• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Would like to convert information captured from a from to MySQL database.

I have a table with a field called "birthday" set to the date, data type.  

I have a form which collects the following:

$month = $_POST["month"];
$day = $_POST["day"]; 
$year = $_POST["year"];	

Open in new window


the format for each is as follows:

Month:
    <option value="01">January</option>, etc.

Day (single digit values for $x are 1, 2, 3, (not 01, 02, 03) etc:
<select name="day" id="day"> <?php 
 for ($x=1; $x<=31; $x++) {
 echo "<option value='$x'>$x</option>";
 }
 ?>
</select>

Open in new window



<input name ="year" value = "1980" type="text">

Open in new window

   
How to write an effective query?

i tried strtotime ("$month $day $year")

but that didn't work.

Thanks.
0
LB1234
Asked:
LB1234
1 Solution
 
GaryCommented:
What are you trying to do?
Take the selected values in the dropdown and save them in the MySQL table?
0
 
LB1234Author Commented:
These values are from different means, but i've already got the values in the variables listed below.  I need to know how to get the variable's values into a format MySQL will accept, and the syntax of the function i need.

$month = $_POST["month"];
$day = $_POST["day"]; 
$year = $_POST["year"];	

Open in new window

0
 
Marco GasiFreelancerCommented:
I didn't test it but this should work:

$d =$year . '-' . $month . '-' . str_pad($day, 2, '0', STR_PAD_LEFT);
 

Open in new window


Then use $d in your query.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
Ray PaseurCommented:
Please take a moment to read this article.  It will save you a ton of work once you know how PHP handles this DATE/TIME stuff.  Pay particular attention to the ISO-8601 format for datetime values.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Next, take a moment to experiment with form input using free-form dates.  You'll quickly conclude that there is no need for elaborate input controls -- just use the PHP tools and your clients will love you for it!.
http://iconoun.com/demo/strtotime.php
0
 
Ray PaseurCommented:
Let's assume that these variables have been filtered and you're satisfied with the contents (they represent integers in the acceptable ranges for the date).

$month = $_POST["month"];
$day = $_POST["day"]; 
$year = $_POST["year"];	
$isodate = date('c', strtotime("$year-$month-$day"));
print_r($isodate);

Open in new window

0
 
LB1234Author Commented:
Awesome Ray!  Thanks. Question though...

My table is listed below; please note the number format.  Your code outputs the following : 2000-10-01T00:00:00+02:00

Will that be an issue?

table desc
0
 
LB1234Author Commented:
Thanks Ray!
0
 
Ray PaseurCommented:
That should not be an issue if the birthday column is defined as DATE or DATETIME.  MySQL leans toward silent truncation of data elements that exceed the width of the column.

Thanks for the points and best of luck with your project, ~Ray
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now