Search code examples
phpstrtotimesetcookie

setcookie + strtotime('tomorrow') give wrong date


Today 05/18/2011 If i do

echo date("H:i m/d/Y", strtotime("tomorrow"));

i get 00:00 05/19/2011 as it should be but if i use the same function in setcookie like:

setcookie("wls_yesterday_review", 'completed', strtotime('tomorrow'),'/','myhost.com');

My cookie will expire on 05/18/2011 at 09:00PM

Im not sure what im doing wrong but i tested different ways and different times and i always get the same result

i want to expire the cookie at 00:00 of the following day. If today is 05/18 10:00am i want it to expire on 05/19 00:00

Thanks guys


Solution

  • strtotime() function returns a timestamp on success, but you need a numerical value, maybe an integer, in setcookie() function. You should calculate the seconds to the end of today and add it to time()+3600*24

    (time()+3600*24)+(mktime(24,0,0)-time());
    

    where mktime(24,0,0)-time() is the number of seconds to the end of the day.

    setcookie("TestCookie", $value, (time()+3600*24)+(mktime(24,0,0)-time()));
    

    Hope this helps.