我正在尝试在数据库中插入一行。所有条目都正确插入,但是日期插入为:
0000-00-00
我试过手动格式化日期:
$updated = $a["updated"][0];
$updated = explode(" ", $updated);
$updated[1] = explode("/", $updated[1]);
$y = date('Y');
$m = $updated[1][1];
$d = $updated[1][0];
$updated = $y . '-' . $m . '-' . $d;
$updated = strtotime( $updated );
echo '<pre>'. $updated .'</pre>';
$upd = date('Y-m-d', $updated);
$a["updated"][0] = $updated;
echo '<pre>'. $upd .'</pre>';
这是 var_dump($a) 的输出:
1342134000
2012-07-13
array(3) {
["metal"]=>
string(6) "Silver"
["am"]=>
array(3) {
[0]=>
string(7) "2748.00"
[1]=>
string(8) "1779.220"
[2]=>
string(8) "2253.200"
}
["updated"]=>
array(1) {
[0]=>
int(1342134000)
}
}
我也尝试过使用 mysql_date_format 但该行根本没有插入:
$updated = mysql_date_format($updated);
这是我的插入查询
mysql_query("INSERT INTO LondonFixes VALUES(null,'" . $a["metal"] . "','AM'," . $a["am"][1] . "," . $a["am"][0] . "," . $a["am"][2] . "," . $a["updated"][0] . ", null)");
这是我的表结构
CREATE TABLE IF NOT EXISTS `LondonFixes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Metal` varchar(10) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`AmPm` varchar(2) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`GBP` decimal(10,5) NOT NULL,
`USD` decimal(10,5) NOT NULL,
`EUR` decimal(10,5) NOT NULL,
`Updated` date NOT NULL,
`TimeStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `Entry` (`Metal`,`AmPm`,`Updated`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=229 ;
可能有一个非常简单的解决方案,但请放轻松!