PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Feb 25, 2020 12:31 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Sep 11, 2011 11:08 am 
Offline
Forum Contributor

Joined: Fri Jan 23, 2009 4:04 pm
Posts: 116
Hi everyone,

I'm getting a bit confused about how cookies work in different timezones and wondered if someone could shed some light? I'm using a now() function like CodeIgniter's that works out the current timestamp in GMT:
Syntax: [ Download ] [ Hide ]
$now = time();
$time = mktime(gmdate('H', $now), gmdate('i', $now), gmdate('s', $now), gmdate('m', $now), gmdate('d', $now), gmdate('Y', $now));

And I'll be setting cookies based on this time:
Syntax: [ Download ] [ Hide ]
$now    = now();       // 2011-09-11 18:00:00 UTC
$expire = $now + 3600; // 2011-09-11 19:00:00 UTC
setcookie('cookie_name', 'cookie_value', $expire);

If the server sets it to expire on 2011-09-11 19:00:00 (UTC), and let's say the user is UTC-2, will the user's browser interpret the expire time as 2011-09-11 19:00:00 or 2011-09-11 17:00:00? In other words, will they be getting sent before/after they should? I'm hoping the browser will be able to work it out but before I code away I just thought I'd check.

Thanks in advance.

P.S. Apologies if this is the wrong place for the question! I'm not sure if this counts as theory / design ..


Top
 Profile  
 
PostPosted: Fri Sep 16, 2011 4:19 pm 
Offline
Forum Commoner

Joined: Tue Aug 23, 2011 8:23 am
Posts: 39
Location: Oslo, Norway
You send the Expires time using GMT time zone. The browser will know the difference between GMT and the local time zone and expire the cookie at the right time. Some relevant information here: http://en.wikipedia.org/wiki/HTTP_cooki ... nd_Max-Age


Top
 Profile  
 
PostPosted: Sun Sep 18, 2011 6:24 am 
Offline
Forum Contributor

Joined: Fri Jan 23, 2009 4:04 pm
Posts: 116
Excellent, thanks for that greip.

I noticed something while I was testing time(), mktime() and gmmktime() which I found a little strange too. It seems (regardless of server timezone) mktime was an hour behind gmmktime:
Syntax: [ Download ] [ Hide ]
$now = time();
$H = gmdate("H", $now);
$i = gmdate("i", $now);
$s = gmdate("s", $now);
$m = gmdate("m", $now);
$d = gmdate("d", $now);
$Y = gmdate("Y", $now);

$mktime = mktime($H, $i, $s, $m, $d, $Y);
$gmtime = gmmktime($H, $i, $s, $m, $d, $Y);

echo date('Y-m-d H:i:s', $mktime); // 2011-09-18 11:09:46
echo date('Y-m-d H:i:s', $gmtime); // 2011-09-18 12:09:46

Did I miss something? For some reason I assumed they'd be the same..


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group