找出PHP或MySQL中两个日期之间的差异

时间:2010-04-12 14:26:22

标签: php mysql

此查询不会返回1月份的记录,但会返回2月份的记录。

SELECT EventAsstCharged,CustomerName,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

如何使用PHP或MySQL查找两个日期之间的年龄?

2009-09-24 21:09:36     2010-03-04 13:24:58

6 个答案:

答案 0 :(得分:6)

<?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";

答案 1 :(得分:4)

您也可以使用DATEDIFF()函数在数据库级别执行此操作。

http://www.w3schools.com/SQl/func_datediff_mysql.asp

答案 2 :(得分:3)

我认为您可以使用this excellent function by Added Bytes来查找它。

echo datediff('yyyy', '2009-09-24 21:09:36', '2010-03-04 13:24:58);

查看功能参数以获取更多信息。

答案 3 :(得分:2)

我发现找到两个日期之间差异的最简单方法是:

<?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";
?>

来源:http://www.phpf1.com/tutorial/php-date-difference.html

答案 4 :(得分:1)

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

答案 5 :(得分:1)

我使用以下MySQL查询修复了票证年龄。

DATEDIFF(CURDATE(),SUBSTR(ticket.created,1,10)) AS ticket_age