[英]Difference in PHP timestamp and mysql timestamp
我對自己編寫的腳本感到非常不滿,我在其中比較了時間戳。 我可以看到時間戳完全不同。
我不明白為什么第二個時間戳比第一個時間戳低,即使時間更長。 如何比較兩個日期?
我正在通過strtotime()函數獲取這些值。
澄清 :我正在使用
$current_timestamp = strtotime(date('Y-m-d H:i:s'));
$till_date_timestamp = strtotime($database_object->till_date);
你確定你做對了嗎?
mysql> select from_unixtime(1349190381), from_unixtime(1349150450);
+---------------------------+---------------------------+
| from_unixtime(1349190381) | from_unixtime(1349150450) |
+---------------------------+---------------------------+
| 2012-10-02 09:06:21 | 2012-10-01 22:00:50 |
+---------------------------+---------------------------+
1 row in set (0.00 sec)
php > echo date('r', 1349190381), "\n", date('r', 1349150450);
Tue, 02 Oct 2012 10:06:21 -0500
Mon, 01 Oct 2012 23:00:50 -0500
您在strtotime通話中使用了什么字符串? 該功能可以誤譯投入,不應該對任何東西,但良好且明確的日期字符串信任。
不要依賴strtotime
! 同樣很明顯,PHP的時區與數據庫的時區不同。 確保時區相同,或者像這樣獲取當前/截止時間戳(以使它們來自一個來源):
SELECT UNIX_TIMESTAMP() AS 'Current' UNIX_TIMESTAMP(`datefield`) AS 'Till' FROM `table`
這是我得到的結果 。 它們不同於您在問題中提到的內容。
碼:
echo strtotime('2012-10-02 03:06:21'); //outputs: 1349147181
echo "\n";
echo strtotime('2012-10-02 04:00:50'); //outputs: 1349150450
編輯:
$till_date_timestamp = strtotime($database_object->till_date);
確保$database_object->till_date
是一個字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.