2012-03-28 2 views
1

mysql TIMEDIFF를 사용하는 적절한 방법은 무엇입니까? 데이터베이스에서 나는 날짜라는 열을 가지고 있으며, 그것은 CURRENT_TIMESTAMP 설정입니다. 나는sql timediff function

답변

3

이것은 출력에 크게 의존 할 수 있습니다.

예를 들어, TIMEDIFF() 출력은시, 분, 초입니다. 따라서 데이터 유형은 TIME으로 제한됩니다.

SECOND의 단위가 주어지면 TIMESTAMPDIFF()이 더 강력한 솔루션입니다.

이 내용은 모두 MySQL Date and Time functions에 잘 설명되어 있습니다.

결국 일반적으로 이러한 계산은 코드로 수행됩니다. 그것은 SQL이 아닙니다. 귀하의 경우에는 PHP. strtotime()에서 시간 소인을 빼서 간단한 산술 연산을 수행 할 수 있습니다. 또는 PHP> = 5.3을 사용하는 경우 DateTime::diff을 사용할 수 있습니다.

제발, 열 키워드, 즉 date의 이름을 지정하지 마십시오.

0

I는 다음과 같이 할 것이라고 사전에

<?php 
    mysql_connect("localhost","root","");//database connection 
    mysql_select_db("beyondmotors"); 

    $result = mysql_query("SELECT * FROM currentrentals"); 

    echo "<table border='2'> 
    <tr> 
    <th>ID</th> 
    <th>Year</th> 
    <th>Make</th> 
    <th>Model</th> 
    <th>First Name</th> 
    <th>Last Name</th> 
    <th>Insurance</th> 
    <th>Date</th> 
    <th>Notes</th> 
    </tr>"; 

    while($row = mysql_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['id'] . "</td>"; 
    echo "<td>" . $row['year'] . "</td>"; 
    echo "<td>" . $row['make'] . "</td>"; 
    echo "<td>" . $row['model'] . "</td>"; 
    echo "<td>" . $row['firstname'] . "</td>"; 
    echo "<td>" . $row['lastname'] . "</td>"; 
    echo "<td>" . $row['insurance'] . "</td>"; 
    echo "<td>" . $row['date'] . "</td>"; 
    echo "<td>" . $row['notes'] . "</td>"; 
     echo ("<td><a href=\"edit_form.php?id=$row[id]\">Edit</a></td></tr>"); 
     echo "</tr>"; 
     } 
    echo "</table>"; 

감사를 연속적으로 지금 CURRENT_TIMESTAMP 사이의 differene을 계산하기 위해 노력하고 .. 모든 행에 그 반향보다 여기에 내 스크립트입니다 해요 :

SELECT (fields), UNIX_TIMESTAMP(date) as date FROM currentrentals 

그리고 나서 $row['date']으로 날짜에 액세스 할 수 있습니다. 이제이 날짜를 쉽게 빼낼 수 있습니다.

$timesince = date(format, time() - $row['date']); 

희망이 있습니다.