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 but it doesn't seem to work for what I'm trying to do - either that or my syntax is wrong.

print "<td>Today you started work at: " ;
if (!isset($_SESSION['started'])){
  $_SESSION['started'] = $_SERVER['REQUEST_TIME']
  print $_SESSION['started'];
}; 
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
E.Ostler
  • 5
  • 6
  • 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
  • bare in mind that this time value is only the time the PHP Session started, if the person changes browser or they clear their browser history or restart thier computer then the value displayed will not be correct WRT when they may have actually started working. – Martin Nov 16 '22 at 16:32

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:i",$_SESSION['started'])

where H is for hour, and i is for minute.

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:i",$_SESSION['started']);
print "</td>" ;
sofname
  • 429
  • 1
  • 5
  • 20
le Mandarin
  • 192
  • 10