nimish wrote:
I have Month, Day and Year fields on my form. When I submit this form
I want to create a valid date from all three fields before inserting
into MYSQL table.
I tried following:
$dob=$Month."-".$Day."-".$Year;
$dob = strtotime($dob);
But it does not update my date field in the table.
I am novice in PHP, do you know where i am making a mistake.
A MySQL date field is in the format YYYY-MM-DD so you need to format it
like so:
$dob = "$Year-$Month-$Day";
and then not do any further conversion.
Note that you should really be using $_POST for values passed from a
form as register_globals is now off by default and you cannot rely on
eg $Month being posted from a form value named "Month".
You also cannot trust any data sent from a web form and need to validate
it first. This can be as simple as casting the value as an integer like
so:
$dob = (int)$_POST['Year'].'-'.(int)$_POST['Month'].(int)$_POST['Day'];
This doesn't actually ensure it is a valid date though; you can test it
with the checkdate() function:
http://www.php.net/checkdate
--
Chris Hope - The Electric Toolbox -
http://www.electrictoolbox.com/