The Official Blog of Patcoola

Im just a dude watching over my parents,
and trying to make cartoons and video games from home.

R&D 2014-10-20: Sync Server Time with JavaScript

, By Patcoola

Using the PHP’s time() function to set the date in JavaScript. Useful for when the Website would like the visitor to be able to view the same date and time that the Website has.

In JavaScript we need to assign the argument zero to new Date(), then use the setTime() function with the server time multiplied by 1000.

Advertisement

Donate Now!

I don't always use the women's bathroom, but when I do, you can bet it really smells. If you like my blog, please consider donating.


Code:

<?php

$serverTime = time()+1; // plus 1 to offset for a delay

?>

<script>

var d = new Date(0); // important must have the value 0
var t = <?php ECHO $serverTime; ?>;

d.setTime(t*1000);

</script>

 

Advertisement

Donate Now!

This might not be the best time, but making a blog is not easy. Would you consider donating?


 

Post Meta

Share Post

 

Comments: Write Comment

Be the first to write a comment.


Leave a Comment


All comments are reviewed before publishing. Comments must be related to the page topic, must not be spam, and must comply with the criminal code of Canada.