我想用PHP设置一个cookie,它必须在月底过期。
我怎样才能得到到月底之前的秒数?
谢谢。
发布于 2010-05-11 22:09:53
您可以使用time()来获取从epoche开始经过的秒数。然后使用strtotime(" date ")获取日期的秒数。减去这两个数,你就得到了秒数之差。
这将给你一个月的最后一秒:
$end = strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')) - 1;这将为您提供以下内容:
$now = time();这将为您提供距离:
$numSecondsUntilEnd = $end - $now;发布于 2010-05-11 22:16:05
如果你使用的是setcookie()函数,那么你实际上不需要cookie的秒数,你需要的是时间戳,当应该过期的时候:
// Works in PHP 5.3+
setcookie("cookie_name", "value", strtotime("first day of next month 0:00"));
// Example without using strtotime(), works in all PHP versions
setcookie("cookie_name", "value", mktime(0, 0, 0, date('n') + 1, 1, date('Y')));发布于 2010-05-11 22:16:52
创建一个月底的时间戳,并从中减去当前时间的时间戳。
// Create a timestamp for the last day of current month
// by creating a date for the 0th day of next month
$eom = mktime(0, 0, 0, date('m', time()) + 1, 0);
// Subtract current time for difference
$diff = $eom - time();https://stackoverflow.com/questions/2811378
复制相似问题