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

eleven0's avatar

Display Date on Pages Site Wide (via Master Layout through Include)

I have a header page, which is included (@include) in a master layout page. I would like to display users day and date on the header row.

As I research, I see that things can be flexible with Carbon.

But My header area does not have a controller (or a route as it is part of all pages) from which I can pass data to view. Does that mean I have to pass Carbon to each view? How would enable/override it so that I can call dates and time from views directly?

If not with Carbon, what's the easiest way for me to show dates, time in Blade views?

0 likes
4 replies
Yakagi's avatar
Yakagi
Best Answer
Level 14

Hello,

You can pass a variable to multiple views by adding this to the boot method of your AppServiceProvider

View::composer('*', function ($view) {
    $now = Carbon::now();
    $view->with('now', $now);
});
Sergiu17's avatar

Why you don't use JavaScript for this?

<p></p>
// js
let target = document.querySelector('p');
setInterval(function() {
    let date = new Date();
    let time = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
  
  target.innerHTML = time;
}, 1000);

basic example here

skoobi's avatar

In the @include file just add

{{ Carbon\Carbon::now()->format('Y-m-d h:m:s') }} or whatever format you want

Please or to participate in this conversation.