简体   繁体   English

将此时间戳插入MySQL时间戳列

[英]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). 我有一系列数据需要使用PHP(Codeigniter)插入到MySQL表中。

Problem: One of the data is a timestamp that looks like 06/01/12 01:43 PM . 问题:其中一个数据是时间戳,看起来像06/01/12 01:43 PM However when I insert it into MySQL timestamp column, it becomes 0000-00-00 00:00:00 . 但是当我将它插入MySQL时间戳列时,它变成了0000-00-00 00:00:00 How can I format the original format so I can insert it correctly into the timestamp col? 如何格式化原始格式,以便将其正确插入时间戳col?

If you wanted to perform the transformation via PHP, it's quite straightforward: 如果你想通过PHP执行转换,那很简单:

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

strtotime docs 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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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