我已經瀏覽了網站上的問題,我認爲我遇到的問題更深入一點。 我試過使用difftime()函數,但它對我來說結果爲0。試圖計算使用(當前php日期 - mySQL時間戳=天差異)
到目前爲止,這是我做了什麼
$currentDate = time();
//echo $printDate;
//echo $currentDate;
$editedDate = date("Y-m-d ", strtotime($printDate));
$editedCurrentTime = date("Y-m-d ", $currentDate);
echo "$editedDate </br>";
echo "$editedCurrentTime </br>";
$timeDiff = ($editedCurrentTime - $editedDate);
echo "$timeDiff </br>";
$numberDays = floor($timeDiff/(60*60*24));
我先保存時間();到currentDate中,printDate具有條目放入數據庫的日期。兩者都格式化,回聲打印正確,就像它應該。第三個回聲是由於某種原因減法變爲0的地方。當我嘗試一個strtotime($ currentDate)時,editedCurrentTime的回聲變爲1970-01-01。我猜這是一個小問題,與他們有不同格式的事實有關,但它仍然困擾着我。任何幫助將大大升值。
下面是輸出的一個片段:
2012-08-01
2012-08-08
0
2012-08-01
2012-08-08
0
2012-08-01
2012-08-08
0
2012-08-02
2012-08-08
0
嘿,所以我試過了從時間戳中減去時間()的方法,但是它也返回錯誤的結果,差異僅僅是當前日期,計算後的$ numberDays仍然爲0。 – user1058359 2012-08-08 14:01:26
如果'$ printDate'已經是時間戳了,試試'$ foo = time() - $ printDate'。如果不是,則需要先將其轉換爲時間戳。數據最初是否存儲在數據庫中? – Whisperity 2012-08-08 14:03:23
$ printDate直接從數據庫中取出,哪一部分需要轉換爲時間戳? – user1058359 2012-08-08 14:11:53