ulrika
ulrika

Reputation: 3

Nodejs check if XX time has passed

In PHP i am using the following to see if 30 minutes have passed:

if(strtotime($rowFromMysql['last_visit']) < (time() -(30*60))) {
        $database->query("UPDATE `visit_count` SET visitCount = visitCount + 1, cookieId = '$sid', last_visit='$idag' WHERE cookieId = '$sid' AND ipaddress = '$ip' AND user_id = '$ssid'");

    }

How would i do the same check in Nodejs?

I am selecting the same data from MySQL but in nodejs instead of PHP but have no idea how to see if 30 minutes have passed.

Any ideas?

Upvotes: 0

Views: 2576

Answers (1)

Vincent Simard
Vincent Simard

Reputation: 544

You could do something similar to your code using Date objects:

var lastVisit = new Date(getLastVisit());

var thirtyMinutes = 30 * 60000; // 60000 being the number of milliseconds in a minute
var now = new Date();
var thirtyMinutesAgo = new Date(now - thirtyMinutes);

if (lastVisit < thirtyMinutesAgo) {
  updateVisitCount();
}

getLastVisit() should query your database and return your last visit date either as a:

  • value: Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).
  • dateString: String value representing a date. The string should be in a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).

Upvotes: 5

Related Questions