Find difference between two dates in PHP or MySQL
This query does not return the records for january, but it returns records for february.
SELECT EventAsstCharged,CustomerNa开发者_高级运维me,EventID ,EventName,EventExpectedCharges,EventActuallyCharged,EventUserCharged,date_format(EventDate,'%d-%m-%Y') as EventDate ,EventTime FROM tblevent WHERE Status=1 AND date_format(EventDate,'%d-%m-%Y') between '01-01-2011' AND '20-02-2011' AND EntryUser=2 AND Status=1 ORDER BY EventID DESC
How to find the age between two dates using PHP or MySQL?
2009-09-24 21:09:36 2010-03-04 13:24:58
<?php
$diff = strtotime('2010-03-04 13:24:58') - strtotime('2009-09-24 21:09:36');
echo "Difference is $diff seconds\n";
$days = floor($diff/(3600*24));
echo "Difference is $days days\n";
You can also do it at the database level using the DATEDIFF()
function.
http://www.w3schools.com/SQl/func_datediff_mysql.asp
You can use this excellent function by Added Bytes to find it, I think.
echo datediff('yyyy', '2009-09-24 21:09:36', '2010-03-04 13:24:58);
Check out the function parameters for more information.
I found that the easiest way to find the difference between two dates is this one:
<?php
// Get current time, or input time like in $date2
$date1 = time();
// Get the timestamp of 2011 July 28
$date2 = mktime(0,0,0,7,28,2011);
?>
<?php
$dateDiff = $date2 - $date1;
$fullDays = floor($dateDiff/(60*60*24));
echo "$fullDays";
?>
Source: http://www.phpf1.com/tutorial/php-date-difference.html
function dateDiff($endDate, $beginDate)
{
$date_part1=explode(" ", $beginDate);
$date_part2=explode(" ", $endDate);
$date_parts1=explode("-", $date_part1[0]);
$date_parts2=explode("-", $date_part2[0]);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
I fixed my ticket age using the following MySQL query.
DATEDIFF(CURDATE(),SUBSTR(ticket.created,1,10)) AS ticket_age
精彩评论