Search code examples
phpsqlmysqldate-manipulation

Push strangely formatted dates into MySQL from PHP


I am sure this must be a common question but I cannot find it anywhere so here it goes:

I have dates in this format: 09/Jul/2003:00:03:48 -0300 and I want to push them into a DATETIME field in MySQL.

What is the easiest way to do this? Pushing them in as is results in them being zeroed.


Solution

  • MySQL doesn't include the timezone in DATETIMEs, but you can use:

    STR_TO_DATE('09/Jul/2003:00:03:48', '%d/%b/%Y:%H:%i:%s')
    

    ...to convert a string representation of a date/time into a DATETIME for storing in the database.

    Reference: