-1

在 PHP 中,如何将格式为 20:13-02-05T13:45:17-0500 的日期/时间字符串转换为有效的 MySQL 日期时间,以便我可以存储它?

到目前为止我已经尝试过

$date_id = date('Y-m-d H:i:s', strtotime($date_id));
4

3 回答 3

1

我认为这应该有效:

$date_to_store = '20:13-02-05T13:45:17-0500';
$date_for_mysql = date( 'Y-m-d H:i:s' , strtotime( $date_to_store ) );

然后在您的查询中,只需插入$date_for_mysql

mysql_query("INSERT INTO `table` (`timestamp`) VALUES ('" . $date_for_mysql . "')");

当然,您应该使用准备好的语句,但这只是为了说明日期转换。

于 2013-02-06T12:36:35.293 回答
0
// convert non-standard year format 20:13 to 2013
$date = preg_replace ('/^(\d{2}):(\d{2})/', '$1$2', $date_id);

$timestamp = strtotime ($date);
echo date ('Y-m-d H:i:s', $timestamp);
于 2013-02-06T12:42:55.190 回答
0
explode(your_time);

这应该为您提供时间块,然后使用日期和 mktime。

date(Your_preferred_format, mktime(chunks_as_per_mktime));
于 2013-02-06T12:31:09.613 回答