0

I have:

$_SESSION['CREATED'] = time(); 

in php.

I'm trying to 'inject' it to jquery:

var sesstime = <?php echo json_encode($_SESSION['CREATED']) ?>;

Now i want to current date:

var timestampjq = (new Date()).getTime();

And how to format these dates ?

I want to use:

if(timestampjq - sesstime > 60) {

}

Thanks

  • Does this answer your question? [Convert a Unix timestamp to time in JavaScript](https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript) – Justinas May 28 '21 at 07:07

1 Answers1

0

Date.getTime() returns milliseconds (documentation), while PHP's time() function returns seconds (documentation), so you can't directly compare them.

If you want to be able to directly compare them, you must first divide the JS timestamp by 1000:

if (timestampjq/1000 - sesstime > 60) {...}

Or if you want to be really precise, you can use PHP's microtime() function too, but need to keep in mind that it returns a float - milliseconds are after the decimal separator):

// In PHP:
$_SESSION['CREATED'] = microtime(true)*1000;

...

// In JS:
if (timestampjq - sesstime > 60*1000) {...}
Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19