2

I have a table with dates in the form off yyyy-mm-dd.

How would I be able to loop through this table and display an echo saying for instance "1 day between these date's"?

I know how to find the difference using this but I don't know how to loop through and get the difference from a mysql table

$interval = $newDate->diff($datetime2); //(http:www.php.net/manual/en/datetime.diff.php)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DIM3NSION
  • 1,681
  • 5
  • 20
  • 38

2 Answers2

2

Tweek the variables for your implementation but this should iterate through the table.

<?PHP


    $db_handle = mysql_connect($server, $user_name, $password);
    $db_found = mysql_select_db($database, $db_handle);


    if ($db_found) {

        $SQL = "SELECT dte FROM tb_name";
        $result = mysql_query($SQL);

    while ($db_field = mysql_fetch_assoc($result)) {
        print date_diff($db_field['dte'], $datetime2) . "<BR>";
        $datetime2=$db_field['dte'];
    }

    mysql_close($db_handle);

}
else {
    print "Database NOT Found ";
    mysql_close($db_handle);
}

?>
eabraham
  • 4,094
  • 1
  • 23
  • 29
  • how would i edit this so instead of having a specific datetime2 variable, it used the previous field in the database – DIM3NSION Apr 18 '12 at 21:48
1

Use DATEDIFF MySql function:

SELECT DATEDIFF(first_date_column, second_date_column) FROM your_table

description from the manual:

DATEDIFF(expr1, expr2) returns expr1 – expr2 expressed as a value in days from one date to the other

or use TIMEDIFF if that better suits your need:

TIMEDIFF(expr1, expr2) returns expr1 – expr2 expressed as a time value

nobody
  • 10,599
  • 4
  • 26
  • 43