将输入日期转换为mysql可读格式

时间:2011-07-19 10:32:14

标签: php mysql jquery-ui

  

可能重复:
  PHP/MySQL - Format date/time

我有输入字段(名称:day1和varchar)以在我的表单中输入日期。它附有一个jquery日历,并以“D,dd M,yy”格式选择日期(我想要点这个并且不能改变格式)。我想转换并保存到mysql日期格式到表中。这怎么可能?

$insert_query = 'insert into '.$this->tablename.'(
            venue,
            day1,
            day2,
            day3,
            day4,
            day5,
            day6,
            day7,
            day8,
            day9,
            day10,
            city,
            contactperson,
            )
            values
            (
            "' . $this->SanitizeForSQL($formvars['venue']) . '",
            "' . $this->SanitizeForSQL($formvars['day1']) . '",
            "' . $this->SanitizeForSQL($formvars['day2']) . '",
            "' . $this->SanitizeForSQL($formvars['day3']) . '",
            "' . $this->SanitizeForSQL($formvars['day4']) . '",
            "' . $this->SanitizeForSQL($formvars['day5']) . '",
            "' . $this->SanitizeForSQL($formvars['day6']) . '",
            "' . $this->SanitizeForSQL($formvars['day7']) . '",
            "' . $this->SanitizeForSQL($formvars['day8']) . '",
            "' . $this->SanitizeForSQL($formvars['day9']) . '",
            "' . $this->SanitizeForSQL($formvars['day10']) . '",
            "' . $this->SanitizeForSQL($formvars['city']) . '",
            "' . $this->SanitizeForSQL($formvars['contactperson']) . '",
            )';

第1天到第10天是要转换的日期...

2 个答案:

答案 0 :(得分:1)

在日期输入中尝试此操作:

$date = date('Y-m-d H:i:s', strtotime($_GET['date_input']));

只要strtotime()从您的输入中选择正确的时间戳,您就可以了。不要忘记逃避它。

答案 1 :(得分:0)

或在sql语句中,您可以这样做:

$insert_query = 'insert into '.$this->tablename.'(             
    venue,             
    day1,             
    day2,             
    day3, 
    (.......) )             
    values             
    ("' . $this->SanitizeForSQL($formvars['venue']) . '",             
     STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day1']) . '","%W, %d %M, %y"),             
     STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day2']) . '","%W, %d %M, %y"),
     STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day1']) . '","%W, %d %M, %y"),   
     (.......) )';

“%W,%d%M,%y”位的格式必须与日历中的日期格式相同...

要查看所有日期函数和日期格式,请参阅以下链接:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

我希望有帮助...