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

I want to convert the date format provided by the user (mm / dd / yyyy) to the MySQL date format (YYYY-mm-dd). Presentation is done using a simple PHP form for the MySQL database.

+3
source share
3 answers
$mysql_date = date('Ymd H:i:s', strtotime($user_date)); 
+10
source

PHP 5.2 alternative method

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

DateTime is a Y38k and friendly timezone.

0
source

An additional method, this time on the SQL side, is to use the conversion method in your sql query:

  CONVERT(VARCHAR(11),$user_date,111) //eg 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 the date format, with a return of 111: 2006/12/30.

0
source

Source: https://habr.com/ru/post/1336055/


All Articles