我使用this code在JavaScript中创建了一个cookie。其实,我改变了代码一点:将GMT日期格式化为PHP中的整数
function setCookie (name,value,days) {
var expires, newValue;
if (days) {
var date = new Date(); // days = 0.0006944444; // testing with one minute
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toString();
newValue = encodeURIComponent(value)+'|'+date+expires;
} else expires = "";
document.cookie = name+"="+(newValue)+"; path=/";
}
所以上面的函数发送encodeURIComponent(value)+'|'+date+expires
的价值。在PHP中我可以做explode('|',$_COOKIE['my-key'])
采用这样的格式日期:
$string_time = "Fri Oct 06 2017 19:34:44 GMT 0300 (Eastern European Summer Time);
现在我需要这个字符串转换为整数来对PHP的time()
整数格式进行比较。
执行以下操作:
$currentTime = date('YmdHis', time());
$expire_time = date('YmdHis', strtotime($string_time));
它实际上输出这样的:
string(14) "19700101000000" // $currentTime
string(14) "20171006162139" // $cookie_time
问题为什么$currentTime
总是相同19700101000000
价值?
这很混乱?很明显,你没有从设置的cookie中获取到期时间,但是从创建UTC日期的脚本中获得某种方式。为什么不把它当作unix时间戳呢? – adeneo
这是我第一次这样做,也许你可以阐明哪些值应该设置为UNIX时间戳? – thednp
你从哪里得到'$ string_time',你是如何得到它到服务器的? – adeneo