I am building a chat for my website and I want to calculate the time difference between the server and the client. I am using the following code to get the time difference and put it in a hidden input field. But it doesn't work.
<script type = "text/javascript">
var time = new Date().getTime()/1000;
$.post('timediff.php',{time:time},function(response){
alert(response);
});
</script>
And in my timediff.php file I have the following code:
<?php
if(isset($_POST['time'])){
$client_time = $_POST['time'];
$server_time = time();
$time_diff = $server_time - $client_time;
echo $time_diff;
}
?>
I tested this on another computer which has the same time setting as the server but the result that I am getting is larger. Am I doing anything wrong? Is there any better way to do this? Thanks in advance.