2

I would like to have a javascript date object having its time set according to the web server.

<script type="text/javascript">

var date=new Date();

date.setTime(<?php echo time() ?>);

alert(date.toString()); //displays the date and time according to the timezone set on the client's computer

</script>

Is the above code reliable?

Many thanks to you all.

3
  • 2
    Reliable in what respect? It's likely to be off a second or two sometimes if a response takes a lot of time, but other than that... it should be okay Commented Feb 15, 2011 at 12:54
  • you want to seek help from NTP ? ntp.org Commented Feb 15, 2011 at 13:20
  • @ajreal: Thanks for suggesting NTP. My web app is not a critical one, it's just for fun, so it should be fine. Commented Feb 15, 2011 at 18:44

1 Answer 1

4

I think it's not.

Since Javascript's setTime() works with milliseconds and PHP's time() works with seconds.

You'll need to add some stuff to make it works ;) I'll let you find out what.

Sign up to request clarification or add additional context in comments.

4 Comments

So won't date.setTime(<?php echo time() ?> * 1000) suffice to solve this difference?
Seriously? You don't want to make the OP thinks by himself or you just like spoon-feeding? Anyway, as @Pekka said, it won't be reliable because of response time.
It's just some unit conversion so the spoon is not really a big one. Thank you so much for pointing out the error in my code :)
more like giving all the help possible but I can see your point. Sometimes those trivial things take hours for newbies to figure out so I prefer to save them those hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.