简体   繁体   中英

Inserting this timestamp into MySQL timestamp column

I have a series of data that I need to insert into a MySQL table using PHP (Codeigniter).

Problem: One of the data is a timestamp that looks like 06/01/12 01:43 PM . However when I insert it into MySQL timestamp column, it becomes 0000-00-00 00:00:00 . How can I format the original format so I can insert it correctly into the timestamp col?

If you wanted to perform the transformation via PHP, it's quite straightforward:

$oldFormat = "06/01/12 01:43 PM";
$newFormat = date("Y-m-d H:i:s", strtotime($oldFormat));

strtotime docs

date docs

你可以使用MySQL的STR_TO_DATE()函数:

INSERT INTO my_table VALUES (STR_TO_DATE('06/01/12 01:43 PM', '%m/%d/%y %r'))

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