Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the date/time come through correctly with PHP/MSSQL?

I recently upgraded the running version of PHP on my server to 5.3.20. Since then, whenever I pull datetimes out of a MSSQL database (mssql_query) I get weirdly wrong dates coming through.

I have checked the locale settings and the default timezone is set to 'Australia/Brisbane', I've also checked the php.ini settings and confirmed that the mssql.datetimeconvert is turned off. The dates seem to come through preformatted (without seconds) when the option is turned on, however when the option is turned off I get a date that looks like this.

mssql.datetimeconvert (off): 2013-07-38 16:00:20

mssql.datetimeconvert (on): Feb 07 2013 09:37PM

Is there a simple fix?

Here is the code that compares output between mysql/mssql sources:

$rs = $db->query("select id, servertime from bobstable where id = 86427420");
$mss_set = mssql_query("select id, servertime from T_bobstable WITH (NOLOCK) where id = 86427420");

$myrow = $rs->fetch_assoc();
$msrow = mssql_fetch_array($mss_set);

var_dump($myrow);
echo "<br>";
var_dump($msrow);

which outputs as follows:

array(2) { ["id"]=> string(8) "86427420" ["servertime"]=> string(19) "2013-02-08 14:00:24" } 
array(4) { [0]=> float(86427420) ["id"]=> float(86427420) [1]=> string(19) "2013-08-39 24:673:0" ["servertime"]=> string(19) "2013-08-39 24:673:0" }

and this is the output that I receive right out of SQL Server Studio

2013-02-08 14:00:24.673

I can understand how it's been formatted (as that part of the problem was answered below) but I can't understand why it's coming through in this format or how to change PHP's configuration to avoid this. Before I upgraded PHP it used to come through as 2013-02-08 14:00:24. Has anyone experienced or seen this happen before?

like image 315
Bob Arezina Avatar asked Nov 12 '22 11:11

Bob Arezina


1 Answers

Found a solution that seems to stick:-

I downgraded the FreeTDS version that was installed on the server from:

freetds.x86_64 0.91-2.el5  to 
freetds.x86_64 0.64-11.el5.centos

And that fixed the dates.

Don't know why.

like image 62
Bob Arezina Avatar answered Nov 15 '22 07:11

Bob Arezina