473,737 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cookie expire time and time zones

Hi
I have asked this question in alt.php as the time() function as used in
setcookie belongs to php - or does it belong equally in the javascript
camp - bit confused about that. Anyway, can anyone here put me straight on
the following:

I had a look at the time() function came across this:

"To clarify, it seems this function returns the time of the computer's clock
and does not do any timezone adjustments to return GMT, so you are given the
local time. The description says that it's supposed to return the number of
seconds from 1970 12am GMT, when the seconds are actually counted from 12am
in the computer's local time.

Hope it saves some confusion. "

This seems to be saying that the time() function retreaves the date in
seconds from 1970 12a.m from the users pc clock. This is great for me, it
means I don't need to worry about time zones. My cookie will expire in
60seconds (time() + 60) regardless of where the user lives. Have I
interpreted things correctly?
thanks
David

Jul 20 '05 #1
6 12939
David Graham wrote on 20 Nov 2003:
Hi
I have asked this question in alt.php as the time() function as
used in setcookie belongs to php - or does it belong equally in
the javascript camp - bit confused about that. Anyway, can
anyone here put me straight on the following:

I had a look at the time() function came across this:

"To clarify, it seems this function returns the time of the
computer's clock and does not do any timezone adjustments to
return GMT, so you are given the local time. The description
says that it's supposed to return the number of seconds from
1970 12am GMT, when the seconds are actually counted from 12am
in the computer's local time.

Hope it saves some confusion. "

This seems to be saying that the time() function retreaves the
date in seconds from 1970 12a.m from the users pc clock. This is
great for me, it means I don't need to worry about time zones.
My cookie will expire in 60seconds (time() + 60) regardless of
where the user lives. Have I interpreted things correctly?


It seems to me that it's saying it returns the client's local time,
and not GMT/UTC, so you *will* have to worry about time-zones: cookie
expiry dates *must* be in GMT/UTC, not local time. However, you can
use the PHP function, gmdate():

// Returns the current date and time, in GMT, in the (cookie) format:
// Wdy, DD-Mon-YY HH:MM:SS GMT
//
gmdate( "D, d-M-Y H:i:s \G\M\T", time());

In JavaScript, you'd just use the Date.toUTCStrin g() method.

Hope that helps,

Mike

--
Michael Winter
M.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #2

"Michael Winter" <M.******@bluey onder.co.uk.inv alid> wrote in message
news:Xn******** *************** ********@193.38 .113.46...
David Graham wrote on 20 Nov 2003:
Hi
I have asked this question in alt.php as the time() function as
used in setcookie belongs to php - or does it belong equally in
the javascript camp - bit confused about that. Anyway, can
anyone here put me straight on the following:

I had a look at the time() function came across this:

"To clarify, it seems this function returns the time of the
computer's clock and does not do any timezone adjustments to
return GMT, so you are given the local time. The description
says that it's supposed to return the number of seconds from
1970 12am GMT, when the seconds are actually counted from 12am
in the computer's local time.

Hope it saves some confusion. "

This seems to be saying that the time() function retreaves the
date in seconds from 1970 12a.m from the users pc clock. This is
great for me, it means I don't need to worry about time zones.
My cookie will expire in 60seconds (time() + 60) regardless of
where the user lives. Have I interpreted things correctly?


It seems to me that it's saying it returns the client's local time,
and not GMT/UTC, so you *will* have to worry about time-zones: cookie
expiry dates *must* be in GMT/UTC, not local time. However, you can
use the PHP function, gmdate():

// Returns the current date and time, in GMT, in the (cookie) format:
// Wdy, DD-Mon-YY HH:MM:SS GMT
//
gmdate( "D, d-M-Y H:i:s \G\M\T", time());

In JavaScript, you'd just use the Date.toUTCStrin g() method.

Hope that helps,

Mike

--
Michael Winter
M.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)


Hi
Why is nothing ever simple.
I have been to the php manual to look up details on gmdate() function. I
note something about the function being broken in MS Windows OS - is this a
serious problem, if so I will stay clear of this function. I have pasted the
line below from the maual

header("Expires : ".gmdate("D , d M Y H:i:s", time() + 3600)." GMT");

I think this is saying - get the corrected date and time - i.e. whatever the
local time may be, correct it to GMT. Then the second argument is get the
lapsed seconds since 1970 Jan 1st and add an hours worth of seconds to this
(time that cookie exists for). Then I get confused as to how this helps me
in setcookie() to set a cookie expire time. Obviously, when I use this stuff
for real I will strip away the header() bit and the concatonated strings at
the start and finish. But the actual detail of how that second argument
combines with the GMT to set an expire time is something I'm not getting at
the moment. Need a bit more help please to understand this.
David



Jul 20 '05 #3
David Graham wrote on 20 Nov 2003:

"Michael Winter" <M.******@bluey onder.co.uk.inv alid> wrote in
message news:Xn******** *************** ********@193.38 .113.46...
David Graham wrote on 20 Nov 2003:
> Hi
> I have asked this question in alt.php as the time() function
> as used in setcookie belongs to php - or does it belong
> equally in the javascript camp - bit confused about that.
> Anyway, can anyone here put me straight on the following:
>
> I had a look at the time() function came across this:
>
> "To clarify, it seems this function returns the time of the
> computer's clock and does not do any timezone adjustments to
> return GMT, so you are given the local time. The description
> says that it's supposed to return the number of seconds from
> 1970 12am GMT, when the seconds are actually counted from
> 12am in the computer's local time.
>
> Hope it saves some confusion. "
>
> This seems to be saying that the time() function retreaves
> the date in seconds from 1970 12a.m from the users pc clock.
> This is great for me, it means I don't need to worry about
> time zones. My cookie will expire in 60seconds (time() + 60)
> regardless of where the user lives. Have I interpreted things
> correctly?


It seems to me that it's saying it returns the client's local
time, and not GMT/UTC, so you *will* have to worry about
time-zones: cookie expiry dates *must* be in GMT/UTC, not local
time. However, you can use the PHP function, gmdate():

// Returns the current date and time, in GMT, in the (cookie)
format: // Wdy, DD-Mon-YY HH:MM:SS GMT
//
gmdate( "D, d-M-Y H:i:s \G\M\T", time());

In JavaScript, you'd just use the Date.toUTCStrin g() method.

Hope that helps,

Mike

--
Michael Winter
M.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)


Hi
Why is nothing ever simple.
I have been to the php manual to look up details on gmdate()
function. I note something about the function being broken in MS
Windows OS - is this a serious problem, if so I will stay clear
of this function. I have pasted the line below from the maual

header("Expires : ".gmdate("D , d M Y H:i:s", time() + 3600)."
GMT");

I think this is saying - get the corrected date and time - i.e.
whatever the local time may be, correct it to GMT. Then the
second argument is get the lapsed seconds since 1970 Jan 1st and
add an hours worth of seconds to this (time that cookie exists
for). Then I get confused as to how this helps me in setcookie()
to set a cookie expire time. Obviously, when I use this stuff
for real I will strip away the header() bit and the concatonated
strings at the start and finish. But the actual detail of how
that second argument combines with the GMT to set an expire time
is something I'm not getting at the moment. Need a bit more help
please to understand this. David


I just looked at the comments on those functions in the PHP online
documentation. It seems that you missed one comment, and
misinterpreted (sort of) your most recent concern:

The poster that suggested time() returns values relative to local
time was apparently mistaken. The comment immediately above it claims
that time() is correct, and the mistaken poster was probably using
date() (which adjusts a timestamp to local time) to display the
return value. I would have suggested that you try it for yourself,
but I see that we're both in the UK, so date() and gmdate() would
return the same time for us. I wrote a small test page and tried
changing my time-zone settings, but it was determined to return
everything in GMT, so I was unable to verify anything.

The problem with a Windows server and gmdate() is when you pass
negative timestamps. This would only be a problem if you intend to
parse dates before 01-Jan-1970 00:00:00 GMT (UNIX Epoch). There is no
way of getting around this (date() too is limited to dates after the
UNIX Epoch).

About the date formatting, use my string ("D, d-M-Y H:i:s \G\M\T") as
it is guaranteed to be the correct format. The 'T' format specifier
could return any string that infers GMT. During my test with gmdate()
and date(), 'T' returned 'GMT Standard Time' - it depends upon the
operating system.

In a nutshell, you needn't really worry about the concerns you've
raised, unless you want to use a date before the UNIX Epoch, and then
there's nothing you can do about it directly anyway. The safest way
would be to use a host that runs Unix or Linux: they're free from
that particular error.

Mike

--
Michael Winter
M.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #4
>
The poster that suggested time() returns values relative to local
time was apparently mistaken. The comment immediately above it claims
that time() is correct, and the mistaken poster was probably using
date() (which adjusts a timestamp to local time) to display the
return value. I would have suggested that you try it for yourself,
but I see that we're both in the UK, so date() and gmdate() would
return the same time for us. I wrote a small test page and tried
changing my time-zone settings, but it was determined to return
everything in GMT, so I was unable to verify anything.

Thanks for all your help Michael.
If the time() function does not return values relative to local time, then
that is probably why I see coders mostly setting their cookies like this

setcookie("cook iename", "cookievalu e", time() + 3600)

i.e. they make no effort to allow for timezones. But that can't be correct -
php has not produced the function gmdate( "D, d-M-Y H:i:s \G\M\T", time());
for no good reason. Any ideas why it is rare to see gmdate() being used?
David

Jul 20 '05 #5
David Graham wrote on 21 Nov 2003:

<snip>
But that can't be correct - php has not produced the function
gmdate( "D, d-M-Y H:i:s \G\M\T", time()); for no good reason. Any
ideas why it is rare to see gmdate() being used?


I couldn't honestly say. The only reason that I might not use
gmdate() is because of the pre-UNIX Epoch problems I mentioned*, but
date() suffers from those problems, too. However, I'm not likely to
write anything that would need to parse dates prior to 1970. In fact,
the only practical things I can see that would need to is a calendar
of some sort, or when parsing someone's birthday in a membership list
(if it's stored as a timestamp, not a string).

If it's going to be used purely for cookies, then I see no reason at
all not to use it. You aren't going to set a cookie to expire prior
to 1970 (setting it to last week, or - even simpler - timestamp 0,
would do that). In fact, you're violating the RFC relating to cookies
by not using it as they must use GMT time.
Mike

P.S. Sorry for repeating myself.

* However, most PHP hosts I know of use UNIX/Linux hosts, which don't
have any problems with the date libraries.

--
Michael Winter
M.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #6
JRS: In article <kd************ ********@newsfe p1-win.server.ntli .net>,
seen in news:comp.lang. javascript, David Graham
<da************ @ntlworld.com> posted at Fri, 21 Nov 2003 23:20:16 :-
If the time() function does not return values relative to local time, then
that is probably why I see coders mostly setting their cookies like this

setcookie("coo kiename", "cookievalu e", time() + 3600)

i.e. they make no effort to allow for timezones. But that can't be correct -
php has not produced the function gmdate( "D, d-M-Y H:i:s \G\M\T", time());
for no good reason. Any ideas why it is rare to see gmdate() being used?


Since this is, AFAICS, a PHP discussion, it should be moved to a PHP
newsgroup or to E-mail.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2457
by: Alexander Ross | last post by:
referring to the code below, will the cookie expire 24 hours after its created, or will it expire 24 hours after the last time I add a variable to the cookie?? if ($_POST){ if (!($_COOKIE)) setcookie("adagame-5q24h",$_POST,time()+60*60*24,"/","",0); else setcookie("adagame-5q24h",$_POST);
16
11318
by: Phil Powell | last post by:
Fourth attempt.. it fails now in login, I check by printing $_COOKIE and there is no value there! Guys, what on earth do I do about this???? Here is the code that sets the cookie: if ($hasLoggedIn && ($row = mysql_fetch_row($query))) { setcookie('nordicnet_registration', $row, 0, '/'); @mysql_free_result($query);
12
18010
by: chrism | last post by:
Hello, I have a pop-up window that I would like to appear in front of the browser home page when a user opens IE. Problem is, I'd like it to never appear again if the user navigates back to the home page during their time using the browser. However, if the user closes the browser, then reopens, the pop-up should appear again. (you may have guessed that this will be used for public access pc's.) I want to try as best I can to catch...
7
7205
by: What-a-Tool | last post by:
How does the expire date work setting it server side with asp. I know with javascript setting it client side it will be set to the clients local time, and therefore expire when the clients local time reaches the set expire-time. But if it is an expire time set on my server in California, and the cookie is put on a computer that is running on London Time, and the expire time is set at the server as 20 minutes from now, the London computer...
4
3821
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript"> if(document.cookie.indexOf("beenHere1=true")!=-1) else
7
1686
by: p2 | last post by:
Hi, I'm setting a cookie with setcookie, and as long as I'm not closing the browser - the cookie is kept and passed to the next page. But as soon as I leave the site or close the browser, the cookie is gone, ans there is nothing in $_COOKIE dor the next time. The cookie is not shown in the cookies folder.
15
3649
by: Oleg Leikin | last post by:
Hi, (newbie question) I've created some simple .NET ASP application that should store cookies at the client machine. According to the documentation cookie expiration time is set via HttpCookie.Expires property, but property value is the time of day on the client. How can I possibly know client local time ?
15
2137
by: Edwin Knoppert | last post by:
I have searched but info is limitted. In my test app i used a non persistant cookie for forms authentication. slidingExpiration is set to true On run and close and rerun the login remains ok. I have a time-out of one minute and indeed, it directs me to the login if i wait to long. The slidingExpiration does it's work also.
2
2707
by: rn5a | last post by:
A web.config file has the following code: <configuration> <system.web> <authentication mode="Forms"> <forms name="NETConnectCookie" loginUrl="Login.aspx"> <credentials passwordFormat="SHA1"/> </forms> </authentication> </system.web>
0
8966
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9331
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9257
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6052
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.