我使用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