I am trying to insert a row into database. All entries insert correctly, however the date is inserted as:
0000-00-00
I have tried formatting the date manually:
$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>';
This is the output of the 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)
}
}
I have also tried using mysql_date_format but the row is not inserted at all:
$updated = mysql_date_format($updated);
This is my insert query
mysql_query("INSERT INTO LondonFixes VALUES(null,'" . $a["metal"] . "','AM'," . $a["am"][1] . "," . $a["am"][0] . "," . $a["am"][2] . "," . $a["updated"][0] . ", null)");
This is my table structure
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 ;
There is probably a very simple solution to this, but go easy on me please!
$a["updated][0]appears to an integer unix timestamp value, rather than a formatted date string. To pass in the integer value, wrap it in aFROM_UNIXTIMEin your SQL statement, -or- pass in the the formatted date string. (See my answer.)