I need to get an accurate timestamp of the request in order to then calculate the session end time
I need the timestamp in the layout to be available globally for some JS script, so right now I store it in the layout file:
// layout.blade.php
<div id="request-timestamp" data-request-timestamp="{{ now()->timestamp }}"></div>
Then I can access it using JS:
const requestTimestamp = document.getElementById("request-timestamp").dataset.requestTimestamp;
But, is there a way to get a more accurate timestamp to the time where Laravel actually saves the session time? Because in my app, there are a few DB connections before it actually gets to the frontend, so it might be less accurate.
Can I somehow send the request timestamp to the layout file in a more accurate way?
2
Answers
I ended up using the
LARAVEL_START
constant that is defined in thepublic/index.php
file, which returns the unix timestamp:And then set it in a div:
That works pretty well.
To ensure that the timestamp is as close as possible to the server-side request processing, you can get it in the controller and pass it directly to the view.
Controller
Script