Skip to content
Advertisement

Current time from SQL Server to PHP for compare

Need get info, is EndDate expired or not (EndDate is row, where typed info about date of buyed user premium)

connectuser2db();
$bile_sql = "SELECT EndDate FROM tbl_rfuser WHERE Serial = '" . $userdata['serial'] . "'";
if (!($bile_result = mssql_query($bile_sql))) {
    echo "Unable to select query the Billing table";
    exit;
 }

$bilel = mssql_fetch_array($bile_result);          
$userdata['Enddate'] = $bilel['EndDate'];
$t_logd = $bilel['EndDate'];

echo $t_logd;

result like Jul 14 2020 02:36:00:000PM (if EndDate in smalldatetime data type)

$time = date("Y-m-d H:i:s");

echo $time;

result like 2020-07-15 14:51:21

If i use $time in this row, it update enddate to curent time:

$t_login = $userdata["lastlogintime"]; // last user login in game (from mssql) Jul 15 2020 02:36:00:000PM
$t_logd = $bilel['EndDate']; // Premium EndDate (from mssql) Jul 14 2020 02:36:00:000PM
$time = date("Y-m-d H:i:s"); // current time (from php) like 2020-07-15 14:51:21
 if( $t_logd <= $t_login ) 
 { connectuser2db();
$user_update = "UPDATE tbl_rfuser SET EndDate='" . $time . "' WHERE Serial= '" . $userdata["serial"] . "'";
 mssql_query($user_update); }

It succesfully compare “lastlogin” and “enddate”, and if account is new or user be online last time before EndDate expired – change EndDate to current time, that succesfully takes from $time. But if user login after EndDate, it not works

Want this compare:

if( $t_logd <= $t_login ) //working now

Change to compare like:

if( $t_logd <= $time ) // not working now

Help please How can I take current time from mssql or change $time for something, that can compare? Tried strtotime, but can’t do it (sql server check it in miliseconds, but my php old and can’t use date(“Y-m-d H:i:s:v”) format)

Advertisement

Answer

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);
?>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement