0

I am using the function :

var date = new Date();

in javascript. it gives the following output

"Sat May 13 2017 22:19:25 GMT+0500 (Pakistan Standard Time)"

Now I will embed this date in a URL and on server side , I will receive this date. On server i have a php script , now in this script i want to compare the date received from javascript client side with current php time and check if the difference between date time zone sent from javascript client side and current php date time zone is greater than 5 minutes or not .

4
  • 3
    You should use unix timestamp instead. Commented May 13, 2017 at 17:32
  • Is time important for comparison? Commented May 13, 2017 at 17:35
  • i also have date time stamp with zone in millisecond in javascript like this....."1494688862301".....so how can i compare this in php ? Commented May 13, 2017 at 17:36
  • @YasharAliabasi , yes time is important.....i need to check if 5 minutes have passed since the user clicked on the link......so in php script i need to check if 5 minutes have passed , then i will expire the link Commented May 13, 2017 at 17:38

1 Answer 1

1

You should use timestamp comparison.

JS:

var timestamp = (new Date()).getTime(); // send this to your php

PHP:

$timestampFromJs = intval($_GET["ts"]); // just made it up
$ts = time() * 1000; // js timestamp is milliseconds
if($ts - $timestampFromJs > 5000*60) ...

UPDATE: If you need timezone safe comparison:

JS:

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

PHP:

$timestampFromJs = intval($_GET["ts"]); // just made it up
$date_utc = new \DateTime("now", new \DateTimeZone("UTC"));
$ts = $date_utc->getTimestamp() * 1000; // js timestamp is milliseconds
if($ts - $timestampFromJs > 5000*60) ...
Sign up to request clarification or add additional context in comments.

6 Comments

does this also compare timezones?
I added timezone check now.
I always thought that timestamp - seconds for Greenwich. Am I wrong?
Thats true. GMT = UTC
The "timezone safe" version is not timezone safe. The first version (which uses UTC) is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.