1

Currently I'm converting all my PHP unix timestamps to work beyond 2k38 issue.

I noticed that setcookie() expire date parameter uses the unix timestamp. Is there a way to set expire date with alternative method, maybe using the DateTime class somehow?

From the PHP document about expire date:

Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.

4

1 回答 1

6

You cannot change the function signature. Well, not at least without fiddling with strange PHP extensions. But since the cookie spec does not use Unix timestamps at all you can simply write your own function and call header() manually:

Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2099 10:18:14 GMT
Set-Cookie: lang=en-US; Max-Age=8640000

... and hope that browsers are able to process the date:

If the expiry-time is later than the last date the user agent can represent, the user agent MAY replace the expiry-time with the last representable date.

Or you can simply use seecookie() anyway. As far as I know, it'll only be an issue in some 32-bit versions of PHP.

于 2016-04-10T14:06:02.340 回答