Why don't you fetch the current timestamp via JS?
How to get more accurate timestamp of the request?
Hello,
In my previous post I asked how to get the request timestamp. So I ended up using Carbon's now() in the layout file:
<div id="request-timestamp" data-request-timestamp="{{ now()->timestamp }}"></div>
Now I can access it via JS:
const requestTimestamp = document.getElementById("request-timestamp").dataset.requestTimestamp;
But, since there are a few DB connections before the actual frontend page loads, I think it won't be the most accurate. Is it possible to somehow set it earlier, as close to the actual request as possible and then pass it to the layout file?
Since I need it in the layout file I can't actually put it in every Controller but I need it to be available on every page that uses the layout
Thanks
If you look inside public/index.php file, there is a LARAVEL_START global constant. It is defined before Laravel is initialized. It is in microtime. You can use it like this:
<div id="request-timestamp" data-request-timestamp="{{ LARAVEL_START }}"></div>
// e.g. LARAVEL_START = 1686246104.7676
const requestDate = new Date(parseFloat(
document.getElementById("request-timestamp").dataset.requestTimestamp
) * 1000);
Please or to participate in this conversation.