将时间插入php表单并提交给mysql


insert time into php form and submit to mysql

我知道这可能比听起来更容易,但我已经在网上搜索了几个小时的解决方案。我想在表单中添加一个简单的html文本框,用户可以在其中输入"到达时间"并提交到mysql。提前谢谢。

将文本框验证为已知形式,例如:

2007-06-05 15:26:0

使用strtotime()将提交并验证的值解析为PHP日期对象,如下所示:

 $timestamp = strtotime($_POST['userinput']);
 $mysql_date = date("Y-m-d H:m:s", $timestamp);

strtotime文档:http://php.net/manual/en/function.strtotime.php

然后将$mysql_date变量放入数据库的datetime列中。

编辑:添加如何验证mysql时间字符串值的代码

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;
}

我不确定正则表达式有多好,我没有写它,而是从以下位置获取它:http://www.dzone.com/snippets/regular-expression-match