Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript clock with server time [duplicate]

Tags:

javascript

php

Possible Duplicate:
Making Live Clock javascript

I want to create a clock, with javascript and php. It should start from a server time (unix timestamp with php) and continue with javascript. How can I create it?

In this topic, he said to use Date.now();, but this function return a timestamp with milliseconds, and php doesn't return milliseconds.

Help me, please.

Thanks.

like image 559
mrdaliri Avatar asked Oct 15 '11 21:10

mrdaliri


2 Answers

You'll have to pass the current server time into the page as it's created, and use that to initialize your javascript clock. Thankfully, JS's Date object will accept a timestamp (in milliseconds) to initalize with, so it's as simple as:

<script type="text/javascript">
    var d = new Date(<?php echo time() * 1000 ?>);
</script>

Note the * 1000 part. PHP's timestamps are in seconds, and JS's are in milliseconds.

like image 159
Marc B Avatar answered Nov 04 '22 11:11

Marc B


PHP has function returning miliseconds: http://php.net/manual/en/function.microtime.php

like image 39
graywolf Avatar answered Nov 04 '22 11:11

graywolf