DIM3NSION DIM3NSION - 3 years ago 79
MySQL Question

Difference between 2 adjacent fields - Date - PHP MYSQL

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)

Answer Source

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);
}

?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download