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

时间:2022-08-19 21:29:56

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

6 个解决方案

#1


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

#2


4  

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

#3


3  

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.

查看函数参数以获得更多信息。

#4


2  

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

#5


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

#6


1  

I fixed my ticket age using the following MySQL query.

我使用下面的MySQL查询确定了我的票龄。

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

#1


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

#2


4  

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

#3


3  

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.

查看函数参数以获得更多信息。

#4


2  

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

#5


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

#6


1  

I fixed my ticket age using the following MySQL query.

我使用下面的MySQL查询确定了我的票龄。

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