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

Ligonsker's avatar

How to get the session timeout date?

Hello

I want to store the session end date timestamp as saved by Laravel and use it in the frontend, but I am not sure where to find it.

Right now I simply save the current date in the beginning of the Controller's method using Carbon::now(); and passing that value to the frontend, but is there a way to actually get the value stored by Laravel?

for example:

// SomeController.php
public function someMethod(Request $request) 
{   
    $now = Carbon::now(); // in separate line to be as close as possible to the request time
    $session_duration = config("session.lifetime");
    $session_timeout = $now->addMinutes($session_duration);
       
    // more code
    
    return view('some-view', ['session_timeout ' => $session_timeout]);
}

But instead can I get the actual session timeout date as stored by Laravel?

Thanks

0 likes
6 replies
Snapey's avatar
Snapey
Best Answer
Level 122

On every request, the session end time will be now() + the session lifetime.

So the default is 120 minutes. At each request, the session lifetime is reset back to 120 minutes.

There is no point in looking (in the session store) because the very nature of receiving a request and checking if the user has a session will reset the timeout.

1 like
Ligonsker's avatar

@Snapey thanks, just wanted to be as close to what Laravel actually stores it as possible, but I will just leave it as I do now right? that should be pretty accurate

Snapey's avatar

@Ligonsker but written more easily as

    $session_timeout = now()->addMinutes( config("session.lifetime"));

probably closer than 10ms.

1 like
Ligonsker's avatar

Thank you. I just realized, I have another issue but I am not sure if I should create another post - I need the session data in the layout file (for each page), because it's a global JS script that updates the request time in a session storage value.

So in my case there is no controller. What is a good way to then store that data in the layout so that I can later retrieve it by JavaScript?

I was thinking to directly output it to some div in the layout:

// layout.blade.php

<div data-session-timeout="{{ now()->addMinutes( config('session.lifetime')) }}"></div>

But is there a better way? (Also, should I make a new post for that?)

Ligonsker's avatar

@Snapey So just include it in the script tag itself:

// layout.blade.php

<body>

<! -- List of Scripts -->
<script src="myScript1.js" data-session-time=data-session-timeout="{{ now()->addMinutes( config('session.lifetime')) }}"></script>
<script src="myScript2.js"></script>
</body>

something like that?

Just thought to put a div somewhere at the top of the body or maybe meta tag in the head so that it will be set at top for other scripts to be able to use it

Please or to participate in this conversation.