我希望设置一个Cookie,并在一天结束时过期
这个可以实现,但是会在24小时后过期:
setcookie('route_upgrade_voted', true, time()+86400);
这样做是不起作用的:
setcookie('route_upgrade_voted', true, mktime(24,0,0) - time());
最简单的方法是:
setcookie('route_upgrade_voted', true, strtotime("tomorrow"));
希望这能帮到你 :)
Cookie过期时间是一个绝对值,基于自1970年1月1日以来的时间。您正在发送一个相对值:“明天的时间减去当前时间”。这基本上转化为现在和午夜之间剩余的秒数,然后被解释为1970年1月1日以前的日期。您根本不需要减去time()
:
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
然后,你就有了“明天午夜”,与今天的当前日期/时间相比。所以:
setcookie(..., mktime(24,0,0));