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:
- Get current server time (Eg: 14:30:00)
- Get the next hour in the server GMT. (Eg: 15:00:00)
- 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.