views:

70

answers:

2

I have a DATETIME for an entry in my database that is upcoming. I want to know the difference in time between the the DATETIME and the current date/time in Days, Hours, Minutes, and Seconds. I thought I might be able to use the date function to do this, but perhaps I am wrong.

This was my approach:

$now = mktime(0, 0, 0, date("m"), date("d"), date("y"));
$entry_datetime = strtotime($row['end_auction']);

$difference = date("G, i, s",($entry_datetime - $now));
echo $difference;

The result i received is

13, 20, 00

but that doesn't make any sense since the DATETIME in $row['end_auction'] is November 28th and today is November 19th, so much more than 13 hours apart.

How do I find the difference between the two values and how do I format it to appear as:

9 days, 10 hours, 32 minutes, and 20 seconds

Thank you in advance!

+3  A: 
function datediff( $date1, $date2 )
{
    $diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );

    return sprintf
    (
        "%d Days, %d Hours, %d Mins, %d Seconds",
        intval( $diff / 86400 ),
        intval( ( $diff % 86400 ) / 3600),
        intval( ( $diff / 60 ) % 60 ),
        intval( $diff % 60 )
    );
}

print datediff( "24th November 2009", "now" ) . "\n";
David
Works like a charm! Thank you very much for writing this up for me!
zeckdude
A: 

You could use UNIX_TIMESTAMP() in mysql to get the timestamp and then operate them with php's date functions

rmontagud