0

我被问到这个面试问题,所以我想我会把它贴在这里看看其他用户会如何回答:

Please write some code which connects to a MySQL database (any host/user/pass), retrieves the current date & time from the database, compares it to the current date & time on the local server (i.e. where the application is running), and reports on the difference. The reporting aspect should be a simple HTML page, so that in theory this script can be put on a web server, set to point to a particular database server, and it would tell us whether the two servers’ times are in sync (or close to being in sync).

这是我放的:

// Connect to database server
$dbhost = 'localhost';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

// Select database
mysql_select_db($dbname) or die(mysql_error());

// Retrieve the current time from the database server
$sql = 'SELECT NOW() AS db_server_time';

// Execute the query
$result = mysql_query($sql) or die(mysql_error());

// Since query has now completed, get the time of the web server
$php_server_time = date("Y-m-d h:m:s");

// Store query results in an array
$row = mysql_fetch_array($result);

// Retrieve time result from the array
$db_server_time = $row['db_server_time'];

echo $db_server_time . '<br />';
echo $php_server_time;

if ($php_server_time != $db_server_time) {
    // Server times are not identical

    echo '<p>Database server and web server are not in sync!</p>';

    // Convert the time stamps into seconds since 01/01/1970
    $php_seconds = strtotime($php_server_time);
    $sql_seconds = strtotime($db_server_time);

    // Subtract smaller number from biggest number to avoid getting a negative result
    if ($php_seconds > $sql_seconds) {
        $time_difference = $php_seconds - $sql_seconds;
    }
    else {
        $time_difference = $sql_seconds - $php_seconds;
    } 

    // convert the time difference in seconds to a formatted string displaying hours, minutes and seconds
    $nice_time_difference = gmdate("H:i:s", $time_difference);

    echo '<p>Time difference between the servers is ' . $nice_time_difference;
}
else {
    // Timestamps are exactly the same
    echo '<p>Database server and web server are in sync with each other!</p>';
}

是的,我知道我使用了已弃用的 mysql_* 函数,但除此之外,您将如何回答,即您将进行哪些更改以及为什么?是否有任何我遗漏的因素需要考虑?

有趣的是,在我的主机帐户上执行时,我的结果似乎总是相隔精确的分钟数:

2012-12-06 11:47:07

2012-12-06 11:12:07

4

4 回答 4

3

这将是我的大部分代码:

$db = new PDO(...);
$dbTime = new DateTime(current($db->query('SELECT NOW()')->fetchAll(PDO::FETCH_COLUMN, 0)));
$myTime = new DateTime();
$diff = $myTime->diff($dbTime);
// do stuff with $diff
于 2012-12-06T17:01:03.797 回答
2
$php_server_time = date("Y-m-d h:m:s");

将时间格式化为年-月-日时:月:秒。这解释了服务器时间似乎是的事实11:12:07。它实际上说它是十二月。数据库时间和服务器时间正好相差 35 分钟,这将是非常令人惊讶的。即使没有“确切地”这个词,也会令人惊讶。

分钟是i格式字符串。


除此之外,时间相差一秒并不一定意味着数据库与服务器不完全同步。这可能只是意味着测量之间经过了一些(任意小)时间。如果要验证同步,可以在服务器上进行两次测量,一次在查询之前,一次在查询之后,并进行范围比较,或者简单地进行增量比较时间(abs(t1-t2)<=1s)

于 2012-12-06T16:56:49.407 回答
1

您放弃了对已弃用mysql_功能的使用,但如果我在求职面试中问这个问题,那将是红旗#1。它会告诉我,要么你没有跟上当前 PHP 的最佳实践,要么你不在乎。

我担心那段代码的另一件事是您没有指定 MySQL 为您提供时间的格式。我想明确指定输出格式,以便您不依赖任何服务器设置。

于 2012-12-06T16:56:07.667 回答
0

会使用 PDO 和查询:

SELECT UNIX_TIMESTAMP()

在代码中:

$time_difference = abs($PDOStatement->fetchColumn() - date('U'));

并且可能会在差异时宣布时间“大致相同”<= 1

于 2012-12-06T17:12:33.513 回答