Getting a date from mssql server

I have this script, kinda the simplest of simplest to post to my db it looks like this: the intressting part is the date i guess as it's that i got problem with

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$head =  $_POST['title'];
$bread = $_POST['html'];
$author = $_POST['selectlist1'];
$postdate = date('Y-m-d H:i:s');
$cat = $_POST['selectlist2'];

$db = new PDO('sqlsrv:server=localhost;Database=blog', '******', '*******');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = 'INSERT INTO dbo.blog_posts (blog_title, blog_post, blog_author, blog_date, blog_category) VALUES (:head, :bread, :author, :postdate, :cat)';

$query = $db->prepare( $sql );
$query->execute( array(':head'=>$head, ':bread'=>$bread, ':author'=>$author, ':postdate'=>$postdate, ':cat'=>$cat ) );

?>

The blog_date column is "datetime" and default value is (getdate())

the interesting part of my script to get the values from the db:

 $result = sqlsrv_query($con,"SELECT TOP 10 * FROM blog_posts ORDER BY blogID DESC");
                while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_BOTH)) 
echo $row['blog_date']

The error:

Catchable fatal error: Object of class DateTime could not be converted to string in C:\inetpub\wwwroot\blog\index.php on line 52

tried many different solutions but i cant seem to get it working as intended, it stores the right values in the db

Upvotes: 0

Views: 40

Answers (1)

Merijndk
Merijndk

Reputation: 1693

try changing this:

echo $row['blog_date'];

to:

echo date_format($row['blog_date'], 'Y-m-d H:i:s');

Upvotes: 1

Related Questions