Object of class DateTime could not be converted to string

后端 未结 7 648
灰色年华
灰色年华 2020-11-27 19:16

I have a table with string values in the format of Friday 20th April 2012 in a field called Film_Release

I am looping through and i want to convert

7条回答
  •  难免孤独
    2020-11-27 20:01

    It's kind of offtopic, but i come here from googling the same error. For me this error appeared when i was selecting datetime field from mssql database and than using it later in php-script. like this:

    $SQL="SELECT Created
    FROM test_table";
    
    $stmt = sqlsrv_query($con, $SQL);
    if( $stmt === false ) {
        die( print_r( sqlsrv_errors(), true));
    }
    
    $Row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
    
    
    $SQL="INSERT INTO another_test_table (datetime_field) VALUES ('".$Row['Created']."')";
    $stmt = sqlsrv_query($con, $SQL);
    if( $stmt === false ) {
        die( print_r( sqlsrv_errors(), true));
    }
    

    the INSERT statement was giving error: Object of class DateTime could not be converted to string

    I realized that you CAN'T just select the datetime from database:

    SELECT Created FROM test_table
    

    BUT you have to use CONVERT for this field:

    SELECT CONVERT(varchar(24),Created) as Created FROM test_table
    

提交回复
热议问题