2011-04-15 88 views
1

我試圖計算從MySQL數據庫中檢索到的時間戳和當前時間之間的差異。如何在PHP中計算MySQL時間戳和當前時間之間的差異

欣賞幫助。

+1

-1:之後我們已經在聊天中告訴過您要嘗試什麼並將您鏈接到http://stackoverflow.com/search?q=[php]+calculate+timestamp+difference和http:// dev.mysql.com/doc/refman/5.5/en/datetime.html你不應該有任何需要問你自己的問題。 – Gordon 2011-04-15 11:56:51

+0

目前還不清楚您的意思是時間戳或unix_timestamp。 – 2011-04-15 11:56:52

+0

爲什麼這個標籤爲Zend-Framework? – Tjorriemorrie 2011-04-17 14:03:17

回答

2

正如@RemoteSojourner所提到的,我使用UNIX時間戳格式(返回時間以秒爲單位)獲得當前時間,我從數據庫中獲得時間戳(使用ORM)並將其轉換爲UNIX timstamp,然後減去兩個時間戳。

  $current_time = strtotime("now"); 
      $last_access_time = strtotime($this->last_access); 
      $inactivity_duration = $current_time - $last_access_time; 
1

這個例子使現在和一個小時前有所不同。

選擇timeDiff測量(NOW(),現在() - 間隔1小時)

+0

+1不會給出答案。 – Khez 2011-04-15 11:57:18

0

從MySQL檢索日期時間像這樣

SELECT UNIX_TIMESTAMP(`time_col`) FROM `tablename`... 

,並將其與time()

+0

啊,我假設(來自php + zend標籤),你想在PHP比較 – 2011-04-15 11:56:32

1

可以使用將時間戳解析爲Unix時間戳的strtotime函數可以在PHP日期函數中進一步解析或格式化。

相關問題