1
In my PHP script, I want to add a date to a column (of type datetime) in MS SQL Server. I set up the connection following this blog post (php5-sybase, php5-odbc, freetds on Ubuntu 14.04/15.10): https://blog.thesysadmins.co.uk/ubuntu-server-connect-to-mssql-via-php.html
My problem is, using the exact same query in a php script as in SQL Management Studio, the query via PHP fails, because the date column is interpreted as NULL. The (abbreviated) query:
INSERT INTO tbl_test (mydate) VALUES (’20-3-2004′)
message: Cannot insert the value NULL into column ‘mydate’ column does not allow nulls. INSERT fails.
As I said, the exact same query runs fine in SQL Management Studio. Now, this query runs without errors:
INSERT INTO tbl_test (mydate) VALUES (’3-20-2004′)
Obviously, the datetime in the Query is interpreted as M-D-Y, but I can't find any option to change that. Surprisingly, it works under Ubuntu 15.10, but not on 14.04, using the same config for apache, php. freetds and php5-odbc use their default values.
Neither SQL Server settings (@@LANGUAGE = Germand, sp_helplanguage German dateformat = dmy) nor php.ini (iconv datetime formats) have M-D-Y configured. mssql.datetimeconvert = On makes no difference to being Off.
tl;dr How do I tell PHP the datetime format isn't MDY but DMY?
You could try using convert() with style 3 for dates without century (yy) or 103 with century (yyyy) - see http://msdn.microsoft.com/en-us/library/ms187928.aspx
– Chris Rogers – 2016-09-09T08:18:38.000Yes, that works. But that's only a workaround (for me at least). Or is it best practice to explicitly CONVERT all dates in the query? Genuinely asking, since I'm not an expert on MSSQL... – NoMad – 2016-09-09T08:26:55.043