phphtmlhtml-head

How to Display Today's Date in PHP Header


Total beginner php question:

I want to put today's date in the header or my website. In my header.php file I have the following code:

<?php @session_start(); 
$z = 1;
include 'functions.php';
$current_page_name = '"'.str_replace(".php","",basename($_SERVER['PHP_SELF'])).'"';
?>

<header>
<nav class="navbar navbar-expand-lg navbar-light fixed-top">
    <div class="container-fluid">
        <a class="navbar-brand js-scroll-trigger" href="index.php">
            <span>My Site</span>
            <img src="img/logo1.png" alt="logo" />
            <p>Tagline</p>
        </a>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a href="about.php">About</a>
                </li>
                <li class="nav-item">
                    XXXXXXXXXXXX
                </li>
            </ul>
        </div>
    </div>
</nav>

From googling, I think I want to insert the following code into the XXXXXXXXXXXX placeholder above<p><b><?php echo "Today's Date ".date("d/M/Y"); ?></b></p>.

Is this the correct approach? Wanted to check here before I started breaking things; any advice super appreciated!


Solution

  • Ok so my only advice is: If you want to display the current date for your user: Don't use php. Its more work to get the users timezone and display the date then just displaying it via javascript.

    if you replace your XXXXXXXXXXXX part with the following script it should help you out:

    <script type="text/javascript">
    document.write(new Date().toLocaleDateString());
    </script>