2009-12-12 28 views
2

我顯示在MySQL柱(reg_time)和當前時間之間的差異,使得最終輸出(一些PHP後在幾分鐘或幾秒鐘顯示或小時或天) be:的MySQL + PHP來減去兩次並顯示differrence

註冊:8小時前

有沒有什麼功能可以做到這一點? 感謝

回答

2
//End date is current date 
//$start_date, lets say sep 05 82 : 00:00:00 

$seconds_dif = mktime(date("G"),date("i"),date("s"),date("m"),date("d"),date("Y")) - mktime('00','00','00','09','05','1982'); 

//Difference in seconds is $seconds_dif 

//Now only math calculation to figure out months/years.. 

echo '<br />Years Difference = '. floor($seconds_dif/365/60/60/24); 
echo '<br />Months Difference = '. floor($seconds_dif/60/60/24/7/4); 
echo '<br />Weeks Difference = '. floor($seconds_dif/60/60/24/7); 
echo '<br />Days Difference = '. floor($seconds_dif/60/60/24); 
echo '<br />Hours Difference = '. floor($seconds_dif/60/60); 
echo '<br />Minutes Difference = '. floor($seconds_dif/60); 

這裏找到:http://www.webhostingtalk.com/showthread.php?t=453668

你需要修改代碼的第3行的數據格式爲: mktime('00' , '00', '00', '09', 「05」,「1982年」)

而且你需要用它來包裝這個代碼轉換成方便的功能:

/** 
* Return string with date time difference between two arguments 
* @param $start_date integer Start date in unix time (seconds from January 1 1970), you can use strtotime('24 November 2003 13:45:54'), for example 
* @param $end_date integer [optional] End date in unix time, by default it equals now 
* @return string 
*/ 
function getDateTimeDifference($start_date, $end_date = time()) { 
    $end_date = mktime(date("G", $end_date),date("i", $end_date),date("s", $end_date),date("m", $end_date),date("d", $end_date),date("Y", $end_date)); 
    $difference = $end_date - mktime(date("G", $start_date),date("i", $start_date),date("s", $start_date),date("m", $start_date),date("d", $start_date),date("Y", $start_date)); 
    $years = floor($seconds_dif/365/60/60/24); 
    $months = floor($seconds_dif/60/60/24/7/4); 
    $weeks = floor($seconds_dif/60/60/24/7); 
    $days = floor($seconds_dif/60/60/24); 
    $hours = floor($seconds_dif/60/60); 
    $minutes = floor($seconds_dif/60); 

    $ret = 'Difference: '; 
    if (!empty($years)) 
     $ret .= $years . ' years, '; 
    if (!empty($months)) 
     $ret .= $months . ' months, '; 
    if (!empty($weeks)) 
     $ret .= $weeks . ' weeks, '; 
    if (!empty($days)) 
     $ret .= $days . ' days, '; 
    if (!empty($hours)) 
     $ret .= $hours . ' hours, '; 
    if (!empty($minutes)) 
     $ret .= $minutes . 'minutes'; 

    return $ret; 
} 
+0

感謝布林, 只是一兩件事。這是我的格式存儲在數據庫中: 2007-03-24 16:22:34 如何改變$ seconds_dif變量以適應? 謝謝.. – almac 2009-12-12 11:24:21

+1

您可以使用strtotime()函數,它以不同的日期和時間格式讀取字符串並返回給您unix timestamp(1970年1月1日起的秒數),也可以將strtotime()的返回值設置爲date()函數作爲第二個參數,並使用修飾符創建非常靈活的日期時間字符串(請參閱完整列表:http://php.net/manual/en/function.date.php) – 2009-12-12 11:36:03

2

看看MySQL的功能timediff()(4.1.1+)和timestampdiff()(5.0+)。
而php方法DateTime::diff()(5.3+)

例如,

SELECT Now(), TIMESTAMPDIFF(HOUR, Now(), '2009-12-12 06:15:34') 

剛回到

「2009-12-12 12:34:00」; 「-6」

在我的機器上。而

$dt = new DateTime('2008-03-18 06:15:34'); 
echo $dt->diff(new DateTime())->format('%y %d %h'); 

(目前)打印

1 25 6