The problem

The implicit conversion of a string/nvarchar to Datetime in MSSQL can be tricky because it depends on your server’s locale settings and the right format of your string. To be sure the right query is generated use a standardized format like ISO-8601 on both sides. But be carefull MSSQL processes ISO-8601 strings like “2013-04-03T07:08:22+0000” not correctly and you get an error.

Example

There is a PHP constant to convert to an ISO8601 Date - cool things ha!

This will give you something like this “2013-04-03T07:08:22+0000”. But this leads to this nasty error. So my first thought was “ok, PHP as always..”, but with a little bit of investigation it turns out that PHP follows the standard.

The solution

1. The “right” PHP format

The problem is the timezone definition and its correct handling, so we convert the PHP time to UTC and remove the definition. You don’t need to set a timezone, if your server is configured to UTC correctly.