将时间插入php表单并提交给mysql
问题描述:
I know this may be easier than it sounds, but ive been searching online for a solution for hours. I want to add a simple html text box to a form where the user can input "arrival time" and submit to mysql. Thanks in advance.
答
Have the text box validated to a known form, example:
2007-06-05 15:26:0
parse that submitted and validated value to a PHP date object using strtotime() like so:
$timestamp = strtotime($_POST['userinput']);
$mysql_date = date("Y-m-d H:m:s", $timestamp);
Documentation for strtotime: http://php.net/manual/en/function.strtotime.php
Then just put the $mysql_date variable into the DB in a datetime column.
Edit: add code for how to validate a value to mysql time string
function TimeIsValid(){
var field = document.getElementById('timeid');
if(field.value.match("/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/"))
return true;
}
I'm not sure how good that regex is, I didn't write it, grabbed it from: http://www.dzone.com/snippets/regular-expression-match