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.

evilReiko
  • 19,501
  • 24
  • 86
  • 102
  • 2
    I don't think so. The question is it is really **now** an issue for you? 2038 is in 18 years. I am 100% convinced your software will be _rewritten and adapted_ til this date... – Peter VARGA Apr 10 '16 at 13:58
  • What is wrong with `setcookie` taking a Unix timestamp? There is nothing that inherently limits them to only go up to the year 2038. If you have 64 bit PHP I would expect setting a cookie expiry beyond 2038 would be fine. – Chris Apr 10 '16 at 13:59
  • 2
    You can re implement the `setcookie` function using the `header` to set them by your self - but I think that you don't understand correctly the problem: after the year 2038 we still can use unix timestamp.. – Federkun Apr 10 '16 at 14:00
  • In fact I've just tested it and set a cookie to expire in 2060 without any issue. – Chris Apr 10 '16 at 14:03
  • @AlBundy I'm asking this question because I'm currently rewriting my software – evilReiko Apr 10 '16 at 14:10

1 Answers1

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360