简体   繁体   English

如何将DDMMMYYYY格式的日期转换为可存储的数据库日期

[英]How do I convert a date of the format DDMMMYYYY into a db storable date

Hi guys my application parses text files where date values are stored in the format: DDMMMYYYY eg: 12MAR2009. 嗨,大家好,我的应用程序解析文本文件,其中日期值以DDMMMYYYY格式存储,例如:12MAR2009。

I noticed that inserting this value as it is into a database datetime value doesn't work at all. 我注意到,将此值原样插入数据库datetime值根本不起作用。 How do I convert this into a datetime value which can be entered into a database. 如何将其转换为可以输入数据库的datetime值。 My application is in php. 我的应用程序在php中。

Something like this should convert your date into MySQL's DATETIME format : 这样的事情应该将您的日期转换为MySQL的DATETIME格式

$date = new DateTime('12MAR2009', new DateTimeZone('America/New_York'));
$mysql_date = $date->format('Y-m-d H:i:s');

You can use strtotime() , but timezone handling with it is kind of a pain. 您可以使用strtotime() ,但是使用它进行时区处理有点麻烦。

$timestamp = strtotime("12MAR2009");

...

INSERT INTO my_table (..., timestamp) VALUES (..., FROM_UNIXTIME($timestamp))

The PHP function strtotime() will take almost any string containing a date/time value, and convert it to a unix timestamp. PHP函数strtotime()几乎接受任何包含日期/时间值的字符串,并将其转换为unix时间戳。

The MySQL function FROM_UNIXTIME() will take a unix timestamp and convert it to MySQL's datetime format. MySQL函数FROM_UNIXTIME()将采用unix时间戳并将其转换为MySQL的日期时间格式。

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

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