How to get real UTC timestamp (client independent)?
Is there a way to get the current UTC timestamp of the real one , independent of the client's time, which might not be set correctly on every client?
I know with JavaScript getTime()
I can get the UTC timestamp in milliseconds. It doesn't depend on the client's timezone, but I think it depends on the client's current time.
I'm trying to make a time tactic that shows UTC real time, so it doesn't matter if the client time is set correctly or not, so that it is correct.
source to share
Get this JSON:
http://json-time.appspot.com/time.json
or
http://www.timeapi.org/utc/now
You can also get it from your server if you have one.
An example of the first (with an interval):
HTML:
<div id="time">Click the button</div>
<button id="gettime">Get the time</button>
JAVASCRIPT (with JQuery):
$("#gettime").click(function() {
setInterval(ajaxCalltoGetTime, 1000);
});
function ajaxCalltoGetTime() {
$.ajax({
type: "GET",
url: 'http://json-time.appspot.com/time.json',
dataType: 'jsonp'
})
.done(function( msg ) {
$("#time").html(msg.hour + ":" + msg.minute + ":" + msg.second );
});
JSFiddler: http://jsfiddle.net/op8xdsrv/
source to share
You can trust your server clock *. Just send the current UNIX timestamp from server to client. Then you can use the constructor in JavaScript . Date(value)
<script>
var serverTime = new Date(<? php echo time(); ?> * 1000);
console.log(
serverTime.getUTCFullYear(),
serverTime.getUTCMonth() + 1,
serverTime.getUTCDate(),
serverTime.getUTCHours(),
serverTime.getUTCMinutes(),
serverTime.getUTCSeconds()
);
// use this time to seed your JavaScript clock
</script>
* Generally speaking, servers periodically synchronize their time with a time provider.
source to share
you can use the timeanddate api to get date and time without depending on the client machine or your server in php. Then send it to javascript.
source to share