I’ve got a really simple problem but I just can’t sort it, fairly new to PHP and MySQL which is why im struggling
Overview – I want to insert an ISO8601 date into MySQL DB table using PHP
I have a text.xml file that has a timestamp (under MeanPublic) in which is ISO8601 format, i.e. YYYY-MM-DDTHH:MM:SS
I can read the file in PHP, I can echo that ISO8601 date, however, if I insert it into MySQL it just logs 0000-00-00 00:00:00. I’ve tried stripping the “T” out of the date but doesnt work.
Ive tried setting the database table field in vchar, char, int, date, datetime, and time, just nothing. However, if I put in the insert a manual date/time it inserts ok.
It’s crucial that it logs as date/time because I need to run date/time queries later on these values
Here’s the code
if( ! $xml = simplexml_load_file('test.xml') )
{
echo 'unable to load XML file';
}
else
{
foreach( $xml as $MeanPublic )
{
$key = split(":", $MeanPublic->Key);
$meanvalue = $MeanPublic->Mean;
$FTSvalue = str_replace('T', ' ', $MeanPublic->Timestamp);
mysql_query("INSERT INTO table (totwhout, wbstamp) VALUES ($meanvalue, $FTSvalue)");
}
You’ve probably already checked this, but are you putting single quotes around the timestamp value when constructing the query? Judging from your posted code, it doesn’t look like you are — MySQL expects the TIMESTAMP values to be passed like strings in that regard.