Chris
Chris

Reputation: 35

How to echo a MSSQL datetime field $row into PHP

I'm trying to create a simple table displaying data from June 2019, however i'm having trouble getting the MSSQL datetime row to eacho out properly. Initially it broke my code and was blank, then i tried using the date() strtotime php function, which now just shows 31 01 1970 for all columns which is incorrect.

Here's a example of the input 2016-07-04 00:00:00.000

Below is my code.

    <?php
$serverName = "SQL,1433";
$connectionInfo = array( "Database"=>"", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo );


$sql = "SELECT TOP 1 * FROM [all sales data]";
$stmt = sqlsrv_query( $conn, $sql);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
?>

<tr>
                    <td><?php echo $row['Order Point Account'];?></td>
                    <td><?php echo $row['Analysis Category'];?></td>
                    <td><?php echo $row['Branch Code'];?></td>
                    <td><?php echo date("d m Y", strtotime($row->date));?></td>
                    <td><?php echo $row['Product Discount Group'];?></td>
                    <td><?php echo $row['End Product Cost of Sale'];?></td>
                    <td><?php echo $row['End Product Other Value'];?></td>
                    <td><?php echo $row['End Product Sales Value'];?></td>
                    <td><?php echo $row['End Product Analysis Quantity'];?></td>
                    <td><?php echo $row['Invoice Account'];?></td>
                    <td><?php echo $row['Magic Number'];?></td>
                    <td><?php echo $row['Order Number'];?></td>
                    <td><?php echo $row['PAC Level 1'];?></td>
                    <td><?php echo $row['PAC Level 2'];?></td>
                    <td><?php echo $row['PAC Level 3'];?></td>
                    <td><?php echo $row['PAC Level 4'];?></td>
                    <td><?php echo $row['Product Code'];?></td>
                    <td><?php echo $row['Salesman 1'];?></td>
                    <td><?php echo $row['Statement Account'];?></td>
                    <td><?php echo $row['Supplier Account'];?></td>
                    <td><?php echo $row['Contact']?></td>
                    <td><?php echo $row['Order Source'];?></td>
                    <td><?php echo $row['User'];?></td>
                    <td><?php echo $row['Customer Order No'];?></td>
                </tr>

            <?php
            }
            ?>


            </tbody>
            </table>
    </body>
</html>

below is the vardump

object(DateTime)#1 (3) { ["date"]=> string(26) "2016-07-04 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" }

Here's the result of print_r($row);

Array ( [Order Point Account] => [Analysis Category] => AD [Branch Code] => 100 [Transaction Date] => DateTime Object ( [date] => 2016-07-04 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/London ) [Product Discount Group] => [End Product Cost of Sale] => 165.19999694824 [End Product Other Value] => 165.2 [End Product Sales Value] => 369.60000610352 [End Product Analysis Quantity] => 1 [Invoice Account] => [Magic Number] => 800713 [Sales Order Line Number] => [Order Number] => 319651 [PAC Level 1] => d [PAC Level 2] => dLB [PAC Level 3] => dLB01 [PAC Level 4] => dLB0106 [Product Code] => [Salesman 1] => 9500 [Statement Account] => [Supplier Account] => [Contact] => fax [Order Source] => FAX [User] => [Customer Order No] => )

Any ideas what i'm doing wrong to get this data to display correctly?

Many thanks!

Upvotes: 1

Views: 853

Answers (1)

devpro
devpro

Reputation: 16117

There are 4 issues in your code:

Issue 1:

You have typo here $row,[Transaction Date].

Issue 2:

Your column name is date not Transaction Date

Issue 3:

Your response in object form not in array format.

Issue 4:

You are using t in date format, which return Number of days in the given month

<td><?php echo date("t m Y", strtotime($row,['Transaction Date']));?></td>

Should be:

<td><?php echo date("d m Y", strtotime($row->date));?></td>

PHP Manual

Upvotes: 0

Related Questions