在PHP

2013-05-31 74 views
-3

之差乘以我有一個臨時表中包含數據庫:在PHP

userid | time    
--------|--------------------- 
1  | 13-01-24 00:04:01 
1  | 13-01-24 00:04:04 
1  | 13-01-24 00:05:06 
2  | 13-01-24 00:06:06 
2  | 13-01-24 00:06:08 

我希望得到一個答案如下:

1, 0 second 
1, 3 second 
1, 62 second 
2, 60 second 
2, 2 second 

我已經試過「TIMEDIFF」腳本(max(time),min(time)),但它不輸出我想要的。請幫助,我正在使用PHP。

+3

我想你應該決定你要寫的語言,你用英語開始,然後去別的地方。 – Ibu

+0

「我試過腳本TIMEDIFF(max(time),min(time))與我想要的腳本不匹配,請問有什麼解決方法,請幫助我使用php」(印度尼西亞語) – Shackrock

+0

Don'我只是說我有這個,我想要這個。請指定你真正想要的輸出(詳細說明你的示例輸出是如何產生的)。我們不是魔術師。 –

回答

1

MySQL的解決方案:

SET @prev_time = null; 
SELECT userid, IFNULL(timediff(`time`,@prev_time), 0) diff, 
     @prev_time := `time` 
FROM mytable 
ORDER BY time 

SQLFIDDLE

+0

謝謝MR。 Barmar,但我很困惑,答案是SQL的腳本,如何在PHP中轉換該答案?我嘗試在PHP中,如下所示:$ prev_waktu = null;$ count1 = mysql_query(「SELECT IFNULL(timediff('waktu','$ prev_waktu'),0)as selisih,'$ prev_waktu':='waktu'FROM mdl_temporer_log2」); while($ jumlah1 = mysql_fetch_array($ count1)){ $ jumlah1_ = $ jumlah1 ['selisih'];}。但該腳本是錯誤的 – pucca

+0

請不要在評論中發佈長代碼,它很難閱讀。如有必要,更新您的問題。 Miro Markarian的答案中有一個PHP解決方案。 – Barmar

0

PHP解決方案:

(記者同樣@Barmar解決方案,但在PHP)

<?php 
$timearray = array("13-01-24 00:04:01", "13-01-24 00:04:04", "13-01-24 00:05:06", "13-01-24 00:06:06", "13-01-24 00:06:08"); 
$prevtimestamp = null; 
foreach($timearray as $it) { 
    $datetime = explode(" ", $it); 
    $date = explode("-", $datetime[0]); 
    $time = explode(":", $datetime[1]); 
    $newtimestamp = mktime($time[0], $time[1], $time[2], $date[1], $date[0], $date[2]); 
    if($prevtimestamp == null) $prevtimestamp = $newtimestamp; 
    echo $newtimestamp - $prevtimestamp . " seconds"; 
    $prevtimestamp = $newtimestamp; 
} 
?>