Convert US date format to ANSI SQL date format (YYYY-mm-dd)

后端 未结 3 1802
梦谈多话
梦谈多话 2020-12-18 11:00

I want to convert user-submitted date format (mm/dd/yyyy) to a MySQL date format (YYYY-mm-dd). Submission is via a simple PHP form direc tto MySQL database.

相关标签:
3条回答
  • 2020-12-18 11:26
    $mysql_date = date('Y-m-d H:i:s', strtotime($user_date));
    
    0 讨论(0)
  • 2020-12-18 11:48

    An alternative method as of PHP 5.2

    $datetime = new DateTime($user_date);
    echo $datetime->format('Y-m-d H:i:s');
    

    DateTime is Y38k and timezone friendly.

    0 讨论(0)
  • 2020-12-18 11:49

    A further method, this time on the SQL side is to use the convert method in your sql query:

        CONVERT(VARCHAR(11),$user_date,111)
    
        //e.g.
        SELECT CONVERT(VARCHAR(11),DATEFIELD,111) AS DATE
        //or
        SET DATEFIELD = CONVERT(VARCHAR(11),'".$user_date."',111)
    

    See: http://www.w3schools.com/sql/func_convert.asp - the number at the end changes the type of date format, with 111 returning: 2006/12/30.

    0 讨论(0)
提交回复
热议问题