Find the difference between two dates in PHP or MySQL

This query does not return records for January, but returns records for February.

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 can I find the age between two dates using PHP or MySQL?

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


source to share


6 answers


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

      



+6


source


You can also do this at the database level using a function DATEDIFF()

.



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

+4


source


You can use this nifty feature with appended bytes to find it I think.

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

      

See function parameters for details.

+3


source


I found that the easiest way to find the difference between two dates is:

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

+2


source


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

      

+1


source


I have set my age for the ticket using the following MySQL query.

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

      

+1


source







All Articles