0

I'm grabbing a date from an API and trying to insert that date into a table in my database. However, no matter what I try it either fails to insert anything (i.e errors) or it just inserts 0000-00-00 00:00:00.

The fieldtype in the table is DATETIME.

The format the date comes in in the API JSON is like follows:

2016-08-28T15:00:00Z

I've tried converting to all sorts. I've tried the following

$date = 2016-08-28T15:00:00Z;
$date = strtotime($date);
$finalDate = date('Y-m-d h:m:s', $date);

echo $finalDate;
//this echos the date in the format I'd expect to see it in the database - 2016-08-28 15:00:00

But when I insert $finalDate into the table it goes it errors. (Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation:)

Any ideas?

user3725781
  • 595
  • 2
  • 7
  • 14
  • 1
    `$date = 2016-08-28T15:00:00Z;` you are quoting that, right? `$date = '2016-08-28T15:00:00Z';` otherwise, that'd surely throw an error. – Funk Forty Niner Nov 12 '16 at 16:57
  • you also have no related code / db schema in regards to mysql/pdo. – Funk Forty Niner Nov 12 '16 at 17:00
  • Ah getting somewhere, thanks! When I insert $finalDate into the table with quotes wrapped around it does indeed insert the date in correctly. However, $finalDate is actually the wrong date! `dateincoming = 2016-08-28T15:00:00Z, finaldate = 2016-08-28 03:08:00` - I'm converting the dateincoming using strtotime, then converting that using the date function as above. – user3725781 Nov 12 '16 at 17:09
  • Solved - I was using h:m:s rather than h:i:s – user3725781 Nov 12 '16 at 17:12
  • Learn more about your issue: [link](http://stackoverflow.com/questions/10286204/the-right-json-date-format) – Md. Khalakuzzaman Khan Nov 12 '16 at 20:10

0 Answers0