0

I have to calculate the expiry time for a Cookie to track hourly unique users on the server. The catch however is that I have to do this for a GMT+ 5:45 server.

Updated Note: The server doesn't care about IPs or anything. We, only want to know if user is unique or not for this hour only! So, we track like this, if you have the cookie, you are RETURNING_USER else UNIQUE_USER and get a cookie for that!

I somehow managed to get the current time on the server using PHP (Thanks to StackOverflow for that.). But I couldn't calculate the time for the expiry.

The algorithm is as follows:

  1. Get current server time (Eg: 14:30:00)
  2. Get the next hour in the server GMT. (Eg: 15:00:00)
  3. Difference between the two time in seconds.

The result in seconds is the expiry time for the COOKIE.

function getNepalTime(){
    // Nepal is in GMT+ 5:45.
    // Setting offset to Nepal time.

    $offset=5*60*60; //converting 5 hours to seconds.
    $offset += 45*60; // Adding 45 mins to offset. 

    $dateFormat="H:i";

    $time=gmdate($dateFormat, time()+$offset);  

    return $time;
}

function getCookieExpiryTime(){

    // Get the current hour in Nepal
    currentTime = getNepalTime();

    // set the next current hour
    nextHour = ??   // How??    

    // subtract the two time
    cookieExpire = nextHour - currentTime;

    // convert it into seconds

    // the result is the cookie expiry time
    // return the time
    return cookieExpire;


}

Could you suggest me on how to solve this. I've been working on it for hours. But, I believe the solution is a simple one. Its just out of reach. Any suggestions or workaround for this problem, sir?

Update:

// Cookie expiry time in terms of seconds
function getCookieExpiryTime(){

    // Get the current seconds elapsed in Nepal
    $currentServerTimeInSeconds = HourMinuteToSeconds(getNepalTime());      

    // find the remainder, and subtract from whole to get expiry time.
    $cookieExpiryTimeInSecods = 3600 - $currentServerTimeInSeconds%3600-1;      

    // add to current time
    $cookieExpiryTimeInSecods+=time();
}

This method solved the problem. Thanks to the community for showing the way. The problem is to get the Cookie expiry time before the next hour. SO if you are in 14:30:00. Then Cookie has 29:59 seconds to expire. I am not tracking any user details in DB for this though.

Ariel
  • 2,471
  • 1
  • 26
  • 43
  • why don't use database for this? – Tariq Husain Feb 15 '16 at 03:57
  • the restriction was to do this in PHP. – Ariel Feb 15 '16 at 04:16
  • you just have to create new database and insert user ip address whenevrer someone visit your website , add a column timestamp in table , thus you can get all the user between last hour ., i can provide some example if you can do like this – Tariq Husain Feb 15 '16 at 04:23
  • The objective was not to insert the timestamp and the ip address of the user in database sir. However, I do understand where you are going with this. A new table that holds a DateTime and a Varchar to hold ip. Yes, this solution is most profound. However, the context is the database only holds information about how many user visited the website during that hour. We only keep the sum of those IP. So, if a new cookie is made, an increment is made to the sum of unique users in that hour. We do not want to keep each records for all the ip and timestamp. We keep 14 for 2pm and keep 20 for user – Ariel Feb 15 '16 at 13:19

0 Answers0