简体   繁体   中英

How to make mysql accept date in a particular format from a migration page?

I have a bunch of excel sheets which I will be uploading to the database; with date in the format 05-Sep-2019. Is there a way to make mysql recognize it as a date rather than a string? I can use replace in excel to replace '-' with '/' if '-' doesn't work.

Thank you.

You can use strtotime function of php and format as you want

$date = '05-Sep-2019';
$newdate = date('Y-m-d',strtotime($date)); //2019-09-05
$newdate = date('Y/m/d',strtotime($date)); //2019/09/05
$newdate = date('d/m/Y',strtotime($date)); //05/09/2019

Or in Mysql

Kindly Note that MySQL stores date in YYYY-MM-DD format by default.

One way is to convert all dates into YYYY-MM-DD format so that it will be compatible with MYSQL.

We can format the date column in excel before exporting it to MYSQL by following the below steps:

  1. Select the column which contains the date
  2. Right click and select format cells
    • Choose date under category (on left-hand side)
    • Then choose custom under category
    • Under type insert the format --> YYYY-MM-DD

You will see all the dates in the columns get converted into the format --> YYYY-MM-DD Then you can import the date into MYSQL safely.

Another way is to keep the column datatype as VARCHAR and use TO_DATE() function to parse the string data into DATE format.

you can convert it into Ymd format

$date = date('Y-m-d',strtotime($your_excel_date)) // 2019-08-29
$date = date('Y/m/d',strtotime($your_excel_date)) // 2019/08/29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM