Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is timezone of cookie stored on client's side?

I need to set cookie that expires after 1 hour using PHP setcookie function. Timezone on my server is set to GMT. How should I set cookie expiry date, to make it working across different client's browser timezones?

like image 483
Karol Avatar asked Apr 19 '12 12:04

Karol


People also ask

How do you determine client time zone?

The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes.

What is user time zone?

The user time zone is a client-specific time zone that can be defined for the user time and user date of each individual user in the user master record. It is contained in the system field sy-zonlo.

How do I get timezone offset?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.


2 Answers

PHP's setcookie() function accepts an integer corresponding to a Unix timestamp value. If your cookie should have a 1 hour time to live, you could just use time() + 3600 for that value. PHP will then create a cookie with expire time like "expires=Fri, 3 Aug 2001 20:47:11 UTC". It is in UTC (GMT) so you do not have to worry about the timezone of the client browser

like image 192
kDjakman Avatar answered Sep 28 '22 20:09

kDjakman


Near as I can tell it shouldn't matter what the client time is. PHP sets the expire time based on the unix timecode. Any variation in that time should reside with the server.

Here is the excerpt from the PHP manual for setcookie():

expire:

The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

like image 37
RockyFord Avatar answered Sep 28 '22 21:09

RockyFord