Paul
Paul

Reputation: 117

MSSQL date to php

I have googled extensively, seen various stackoverflow solutions, but none work here. Not sure why. Help would be great!

The date is stored in a smalldatetime field in MSSQL. The value of this is seen as "2015-11-27 00:00:00". This seems the same format as a date in mySQL. The example below works if the value is pasted in, but not the value from the recordset.

    //gives correct result, 11/27/2015
    $TravDate = date("m/d/Y", strtotime("2015-11-27 00:00:00"));

    //gives wrong result, 12/31/1969
    $TravDate = date("m/d/Y", strtotime($obj->TravDate));

Upvotes: 2

Views: 4577

Answers (3)

Paul
Paul

Reputation: 117

Solution found. I gave up using PHP method above: it worked for all other variables in the recordset except the date field TravDate, which is not null.

Solution involved changing the SQL query by converting the date field as follows:- CONVERT(varchar(10), TravDate, 20) AS TravDate (php echo giving 11/27/2015). This solution found in stackoverflow.

Upvotes: 0

Subin Thomas
Subin Thomas

Reputation: 1416

Please try

  $datetime = new DateTime( $obj->travdate); //create datetime object with received data
    $reform = $datetime->format('Y M d');   //do reformat as required

Refer to http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/

Upvotes: 2

Be sure the $obj->TravDate serialized to string is correct, probably solved value is not correct. Try firstly stringify and after use strtotime() function.

Upvotes: 0

Related Questions