Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

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

0 likes
5 replies
Ligonsker's avatar

@ramonrietdijk So you suggest maybe saving the timestamp somewhere in a session, then using JS to get that? And placing that session as close as possible to the request before everything? Maybe in the AppServiceProvider?

Niush's avatar
Niush
Best Answer
Level 50

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);
1 like
Ligonsker's avatar

@Niush That's pretty good! I compared against the page load time in the front and I can actually get the real difference between the back and front now, 1-2 seconds. awesome, I will use that, thank you!

Please or to participate in this conversation.