Reputation: 12272
I want to set a cookie and have it expire at the end of the day
This works, but expires after 24 hours:
setcookie('route_upgrade_voted', true, time()+86400);
This does not work:
setcookie('route_upgrade_voted', true, mktime(24,0,0) - time());
Upvotes: 3
Views: 5938
Reputation: 548
The easiest would be:
setcookie('route_upgrade_voted', true, strtotime("tomorrow"));
I hope this helps :)
Upvotes: 12
Reputation: 360762
Cookie expiry times are an ABSOLUTE value, based on time since jan 1/1970. You're sending over a relative one: "tomorrow's time minus the current time". This translates into basically the number of seconds left between now and midnight, which is then interpreted as a date back in Jan 1/1970. You don't need to subtract time()
at all:
echo date('r', mktime(24,0,0)), ' ', date('r');
^--note: no subtraction
Tue, 28 Jan 2014 00:00:00 -0600 Mon, 27 Jan 2014 13:30:33 -0600
And poof, you have "tomorrow at midnight", v.s. today's current date/time. So:
setcookie(..., mktime(24,0,0));
Upvotes: 2