Current time from SQL Server to PHP for compare

Solution:

Note, that the MSSQL PHP extension is not available anymore on Windows with PHP 5.3 and removed in PHP 7.0.0, but if you want a solution, you may try the following:

  • Specify how the datetime values are returned from the SQL Server by setting the mssql.datetimeconvert option in your php.ini file to Off. When this option is On the datetime values are returned converted to SQL server settings. When this option is Off the datetime values are returned in YYYY-MM-DD hh:mm:ss format.
  • Use date_create() to create and compare the dates as PHP datetime objects.

The next simplified example demonstrates this approach (tested with PHP 5.2.9):

<?php
// Connection
$server   = 'server,port';
$username = 'username';
$password = 'password';
$database = 'database';
$conn = mssql_connect($server, $username, $password);
if (!$conn) {
    echo "Error (sqlsrv_connect): ".mssql_get_last_message();
    exit;
}
mssql_select_db($database, $conn);

// Statement
$sql = "SELECT DATEADD(day, -1, GETDATE()) AS [DateTime]";
$stmt = mssql_query($sql, $conn);
if (!$stmt) {
    echo "Error (sqlsrv_query): ".mssql_get_last_message();
    exit;
}

// Results
while ($row = mssql_fetch_assoc($stmt)) {
    $datetime1 = date_create($row['DateTime']);
}
$datetime2 = date_create();

// Compare dates
echo ($datetime1 <= $datetime2) ? 'Result: <=' : 'Result: >';

// End
mssql_free_result($stmt);
mssql_close($conn);
?>

Answer

Solution:

Just use GETDATE:

UPDATE tbl_rfuser SET EndDate=GETDATE() WHERE Serial= ...

Source