0

I'm trying to display a session start time on a page in this format

"Today you started work at: 00:00"

I found a session start time if statement here on stack overflow but it doesn't seem to work for what I'm trying to do, either that or my syntax is wrong.. not sure, I'm new to PHP any help would be appreciated.

    print "<td>Today you started work at: " ;
    if (!isset($_SESSION['started'])){
      $_SESSION['started'] = $_SERVER['REQUEST_TIME']
      print $_SESSION['started'];
}; 
jjmerelo
  • 19,108
  • 5
  • 33
  • 72
E.Ostler
  • 5
  • 5
  • Use it as `$_SESSION['started'] = (!isset($_SESSION['started'] ? time() : $_SESSION['started']);` – Aman Agarwal May 18 '18 at 15:38
  • Possible duplicate of [Check if PHP session has already started](https://stackoverflow.com/questions/6249707/check-if-php-session-has-already-started) –  May 18 '18 at 15:40

1 Answers1

0

Never forget to add the ; at the end of the line instruction.

$_SESSION['started'] = $_SERVER['REQUEST_TIME']

This code will only show a value if the session starts for the first time. because isset($_SESSION['started'])) will be false.

In this case, it will only display the timesamp of the the request time.

$_SERVER['REQUEST_TIME']

You must use the date() function for time formatting

date("H:s",$_SESSION['started'])

Never forget also to add the session_start() function before any printing.

Always close an open HTML tag

print "<td>Today you started work at: " ;
...
print "</td>" ;

Here is a correction:

session_start();
print "<td>Today you started work at: " ;
if (!isset($_SESSION['started'])){
    $_SESSION['started'] = $_SERVER['REQUEST_TIME'];
}
print date("H:s",$_SESSION['started']);
print "</td>" ;
le Mandarin
  • 171
  • 7