Muhammad Atieh
Muhammad Atieh

Reputation: 49

Countdown timer get time from client's machine, need server time

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

Answers (2)

Karim Magdy Mansour
Karim Magdy Mansour

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

jtheman
jtheman

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

Related Questions