如何显示第二次打开`welcome.php`的访问时间?

There are two php file :welcome.php and check.php.
The welcome.php is as below.

<?php
session_start();
if($_COOKIE["user"]["xm"] <> "")
{
    $visnum = intval($_COOKIE["user"]["num"])+1;
    setcookie("user[num]" , $visnum , time()+3600);
    setcookie("user[dt]" , date("Y-m-d h:i:s") ,time()+3600);
    echo "welcome  " . $_COOKIE["user"]["xm"];
    echo  "<br />This is your " . $visnum . "  th" . "  visit my website";
    echo  "<br />The latest visit time is  " .$_COOKIE["user"]["dt"] ;

}
else
{
    echo
        '<html><body><form method="post"  action="check.php">
           user: &nbsp;&nbsp;<input type="text" name="xm" size="12"><br />                        
           password:&nbsp;&nbsp;<input type="password" name="Pwd" size="12"><br />
          <input type="submit" value="login"><br />
        </form></body></html>';
}
?>

The check.php is as below.

<?php
session_start();
if($_POST["xm"] == "admin" && $_POST["Pwd"] == "123")
{
    setcookie("user[xm]" , $_POST["xm"] , time()+3600);
    setcookie("user[dt]" , date("Y-m-d h:i:s") , time()+3600);
    echo  $_POST["xm"] . "  :Your first visit  ";
}
else
{
    echo  "<script>alert('user name or password is wrong');location.href = 'welcome.php';</script>";
}
?>

The check.php will show admin :Your first visit when to input admin in user and 123 in password in welcome.php.

  admin :Your first visit 

This is your first time to click 127.0.0.1/welcome.php

Then to click 127.0.0.1/welcome.php in browser for the second time,the output is as below:

welcome admin
This is your 2 th visit my website
The latest visit time is

Notice:time stamp is not in the above output.
To click 127.0.0.1/welcome.php in browser for the third time,the output is as below:

welcome admin
This is your 3 th visit my website
The latest visit time is 2018-08-11 12:09:36

How to show visit time for the second time to open 127.0.0.1/welcome.php in my browser?

Method1:use cookie.
Rewrite welcome.php as below.

<?php
session_start();
if($_COOKIE["user"]["xm"] <> "")
{
    $visnum = intval($_COOKIE["user"]["num"])+1;
    setcookie("user[num]" , $visnum , time()+3600);
    echo "welcome  " . $_COOKIE["user"]["xm"];
    echo  "<br />This is your " . $visnum . "  th" . "  visit my website";
    echo  "<br />The latest visit time is  " .$_COOKIE["user"]["dt"] ;
    setcookie("user[dt]" , date("Y-m-d h:i:s") ,time()+3600);
}
else
{
    echo
        '<html><body><form method="post"  action="check.php">
           user: &nbsp;&nbsp;<input type="text" name="xm" size="12"><br />                        
           password:&nbsp;&nbsp;<input type="password" name="Pwd" size="12"><br />
          <input type="submit" value="login"><br />
        </form></body></html>';
}
?>

Method2: use session.
Thanks for K Sudbury's reminder.
Rewrite welcome.php as below.

<?php
session_start();
if($_COOKIE["user"]["xm"] <> "")
{
    $visnum = intval($_COOKIE["user"]["num"])+1;
    $expire = intval($_COOKIE["user"]["expire"]);
    setcookie("user[num]" , $visnum , time()+3600*$expire);
    echo "welcome  " . $_COOKIE["user"]["xm"];
    echo  "<br />This is your " . $visnum . "  th" . "  visit my website";
    echo  "<br />The latest visit time is  " .$_SESSION["visit"] ;
    $_SESSION["visit"] = date("Y-m-d h:i:s");
}
else
{
    echo
        '<html><body><form method="post"  action="check.php">
           user: &nbsp;&nbsp;<input type="text" name="xm" size="12"><br />                        
           password:&nbsp;&nbsp;<input type="password" name="Pwd" size="12"><br />
          <input type="submit" value="login"><br />
        </form></body></html>';
}
?>

Rewrite check.php as below.

<?php
session_start();
if($_POST["xm"] == "admin" && $_POST["Pwd"] == "123")
{
    setcookie("user[xm]" , $_POST["xm"] , time()+3600);
    setcookie("user[expire]" ,1 , time()+3600);
    setcookie("user[dt]" , date("Y-m-d h:i:s") , time()+3600*$expire);
    $_SESSION["visit"] = date("Y-m-d h:i:s");
    echo  $_POST["xm"] . "  :Your first visit  ";
}
else
{
    echo  "<script>alert('user name or password is wrong');location.href = 'welcome.php';</script>";
}
?>
session_start();

if(!isset($_SESSION["visit"])) {
    $_SESSION["visit"] = date("Y-m-d h:i:s");
}else{
    echo "Your first visit was {$_SESSION["visit"]}";
}

Try using Sessions

isset will check if it has a value, if not then it will set the time. However, if it is set then it will show the time.