Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:03:50+00:00 2026-06-15T14:03:50+00:00

I was asked this interview question so thought I would post it here to

  • 0

I was asked this interview question so thought I would post it here to see how other users would answer:

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).

This is what I put:

// 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>';
}

Yes, I know that I have used the deprecated mysql_* functions but that aside, how would you have answered, i.e. what changes would you make and why? Are there any factors I have omitted which I should take into consideration?

The interesting thing is that my results always seem to be an exact number of minutes apart when executed on my hosting account:

2012-12-06 11:47:07

2012-12-06 11:12:07

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:03:51+00:00Added an answer on June 15, 2026 at 2:03 pm

    This would have been the bulk of my code:

    $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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked this question in some interview. I was required to write code
I was asked this as interview question. Couldn't answer. Write a C program to
Recently I was asked this question in an interview. I gave an answer in
Interview Question I have been asked this question in an interview, and the answer
In an interview for some company, I was asked this question. What design patterns
I was asked this question in an interview and I said the answer was
This was an interview question which was asked to me. Please do not penalize
Its a interview question. Interviewer asked this basic shell script question when he understand
This is an interview question asked a month ago.... Do session use cookies? If
I was asked this question in a job interview, and I'd like to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.