I am trying to set a cookie that will expire at midnight, what is the easiest was to do this?
mktime()
heres what u do
a) figure out what the time difference is between NOW and midnight and store it in a variable $tillmidnight
b) setcookie("NAME","VALUE", time() + $tillmidnight);
I know that but how do I get the variable?
setcookie('name', 'value', mktime(23, 59, 59));
Expires at 23:59:59 or 11:59:59 pm of today..
well thats where the learning comes in... look into mktime() to see how to make a time stamp for midnight @ the current day... then subtract from that time() and youll have the difference
edit: or do it sun's way
The given time has to be the number of seconds since Jan 1st 1970, and NOT the time difference between expiration and now...
This will set it to expire at midnight:
setcookie('name', 'value', strtotime('tomorrow midnight'));
suntra wrote:setcookie('name', 'value', mktime(23, 59, 59)); Expires at 23:59:59 or 11:59:59 pm of today..
Or to make it exactly midnight (in case that one second matters 😉 ):
setcookie('name', 'value', mktime(23, 59, 59) + 1);
Or:
setcookie('name', 'value', mktime(24, 0, 0));