Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare server and client timestamps
// Generate a timestamp (in seconds) on the server. This won't change if the page is served from a cache.
var serverTimestamp = <?php echo time(); ?>;
// Create a new Date object from the local date and time on the client.
var localDate = new Date();
// Convert the local date and time to Universal Time (same as the server).
var localUTCString = localDate.toUTCString();
// Create a new Date object from the UTC date and time on the client.
var UTCDate = new Date(localUTCString);
// Generate a timestamp (in seconds) from the UTC date and time on the client.
var clientTimestamp = UTCDate.getTime() / 1000;
// Compare the server-generated timestamp to the client-generated timestamp.
if (clientTimestamp - serverTimestamp > (60 * 5)) {
document.querySelector('main').insertAdjacentHTML('afterbegin',`
<p class="feedback">
<button onclick="this.parentNode.remove()">dismiss</button>
This page might be out of date. You can try <a href="javascript:window.location=window.location.href">refreshing</a>.
</p>
`);
}
@adactio

This comment has been minimized.

Copy link
Owner Author

commented May 16, 2019

This goes in a script element at the bottom of a PHP page.

@RinatValiullov

This comment has been minimized.

Copy link

commented Jul 12, 2019

Interesting attempt 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.