0

I have date in my db and I'm trying to fetch that date and after get the difference from current date and it's working fine. I got the difference datetime, but when I'm trying to update the difference value into sql I'm getting below error:

Error " as "Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -16 [code] => -16 [2] => An invalid PHP type for parameter 1 was specified. [message]"

I don't know what data type I need to use to store the difference datetime in sql. I hope below code is correct.

Please help me.

Here is my code ebreport database where I fetch date and ebtest database I'm trying to update value into btime column. I've tried Varchar(Max), date, time, datetime and int.

<?php


    date_default_timezone_set('Asia/Kolkata');

       function get_format($df) {
       $str = '';
       $str .= ($df->invert == 1) ? ' - ' : '';
       if ($df->y > 0) {
        // years
        $str .= ($df->y > 1) ? $df->y . ' Years ' : $df->y . ' Year ';
    } if ($df->m > 0) {
        // month
        $str .= ($df->m > 1) ? $df->m . ' Months ' : $df->m . ' Month ';
    } if ($df->d > 0) {
        // days
        $str .= ($df->d > 1) ? $df->d . ' Days ' : $df->d . ' Day ';
    } if ($df->h > 0) {
        // hours
        $str .= ($df->h > 1) ? $df->h . ':': $df->h . ':';
    } if ($df->i > 0) {
        // minutes
        $str .= ($df->i > 1) ? $df->i . ':' : $df->i . ':';
    } if ($df->s > 0) {
        // seconds
        $str .= ($df->s > 1) ? $df->s . '': $df->s . '';
    }

    echo $str;
}
    $serverName = "MAHATHERMOHAMED\MAHATHER";
    $connectionInfo = array( "Database"=>"icd", 'ReturnDatesAsStrings'=> true, "CharacterSet" => 'utf-8');
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    if( $conn ) 
    {

    }
    else
    {
        die( print_r( sqlsrv_errors(), true));
    }

    $sql="select * from ebreport";
    $params = array();
    $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
    $stmt = sqlsrv_query( $conn, $sql , $params, $options );
    $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);

    $date1 = new DateTime($row[bstart]);
    echo $date1->format("g:i:s A")."<br>";

    $date2 = new DateTime("now");
    echo $date2->format("g:i:s A")."<br>";

    $diff = $date1->diff($date2);
    print $diff->format("%H:%I:%S");

    $query2 = "UPDATE ebtest SET btime=(?) WHERE (ebtype ='Training')";
    $var2 = array($diff);
    $stm2 = sqlsrv_query( $conn, $query2, $var2);
    if( $stm2 == false )
    {
        die( print_r( sqlsrv_errors(), true));
    }
    else
    {
        echo " query inserted";
    }
?>
  • try doing this and then insert [link](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php#answer-676828) – Rizwan Taj Oct 26 '15 at 04:28
  • Thanks @ Rizwan Taj,but the problem I'm facing here is when I'm trying to update difference value of datetime , – Mahather Mohamed Oct 26 '15 at 04:38
  • what format of value you are saving in mysql ? – Rizwan Taj Oct 26 '15 at 04:49
  • I am saving time format into sql , I'm getting time difference from above code print $diff->format("%H:%I:%S"); So I want to update that value into sql @ Rizwan Taj – Mahather Mohamed Oct 26 '15 at 05:56
  • I would store the difference in seconds as an integer. If you need to display it in your app as Hours:Minutes:Seconds, you can format it that way at query time. – Tab Alleman Oct 26 '15 at 13:25

0 Answers0