0

Possible Duplicate:
convert date string to mysql datetime field

I have a DateTime column in MySQL database. I would like to convert Form text field data on POST (string) to DateTime before inserting into the database. I appreciate any suggestions.

1

4 Answers 4

1

Depends entirely on what format your POST data is in. Quick/dirty/probably-will-blow-up-and-steal-your-belongings method is

$date = date('Y-m-d H:i:s', strtotime($_POST['yourfield']));
Sign up to request clarification or add additional context in comments.

Comments

1

date() function.

You can find the function in php.net

Comments

0

It depends on format of MySQL date time field and the format of the form field. Normally db datetime is Year-Month-day Hour:min:sec for example, "2011-10-24 14:25:20". Now to convert form submitted date string to datetime, do the following:

$date = strtotime($_POST['form_date']);

$DatabaseDate = date("Y-m-d H:i:s", $date);

Now you can use the above $DatabaseDate to insert datetime into database:

@mysq_query("INSERT INTO TEST_TABLE (id, date_test) VALUES (2, '$DatabaseDate')");

Hope it helps.

Comments

0

instead of strtotime($_POST['yourfield']) use the function mktime, in that way you dont need to worry about the string format

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.