Reputation: 49
I'm using this javascript code in order to calculate the difference between the current time and targeted time.
the counter is working fine, but according to client time, not Server side time.
Here is the code I'm using:
function StartCountDown(myDiv,myTargetDate)
{
var dthen = new Date(myTargetDate);
var dnow = new Date();
ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(myDiv,gsecs);
}
how to get the server time, not client local machine time?
Upvotes: 0
Views: 329
Reputation: 316
Well all you need to do .. is print from php the current time from the server in a javascript tag... set that time as your date "dthen" variable... here it goes:
<?php
print '<script type="text/javascript">var dthen = '.time().';</script>';
?>
//Then your code in another script tag
Upvotes: 1
Reputation: 7491
You could echo the time as an php function to your script
var time = "<?php echo date('Y-m-d H:i:s')?>";
or any date/time format you like. But then of course this would only get server time when page is first loaded. You coud try an appoach like here: http://www.roseindia.net/ajax/jquery/jqueryservertime.shtml to load the time with ajax to your page.
Upvotes: 1