How do I find the difference between a datetime type and the current date/time in php?
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'] i开发者_如何学JAVAs 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!
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";
You could use UNIX_TIMESTAMP()
in mysql to get the timestamp and then operate them with php's date functions
精彩评论