简体   繁体   English

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

[英]Find difference between two dates in PHP or MySQL

This query does not return the records for january, but it returns records for february. 此查询不会返回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

How to find the age between two dates using PHP or MySQL? 如何使用PHP或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. 您也可以使用DATEDIFF()函数在数据库级别执行此操作。

http://www.w3schools.com/SQl/func_datediff_mysql.asp 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 资料来源: 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;
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM