Possible Duplicate:
How to calculate the difference between two dates using PHP?
Date Difference in php?
I have two dates in a variable like
$fdate = "2011-09-01"
$ldate = "2012-06-06"
Now I need the difference in months between them.
For example, the answer should be 10 if you calculate this from month 09 (September) to 06 (June) of next year - you'll get 10 as result.
How can I do this in PHP?
php $sdate = "1981-11-04"; $edate = "2013-09-04"; $date_diff = abs(strtotime($edate) - strtotime($sdate)); $years = floor($date_diff / (365*60*60*24)); $months = floor(($date_diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($date_diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); printf("%d ...
To calculate the difference between two dates in PHP, call date_diff() date/time function, and pass the two dates as argument to it. date_diff() function returns a DateInterval object, or FALSE if calculating the difference is not successful.
*You may also use the DATEDIFF function. COMPUTE days2 = DATEDIFF(date2,date1,"days"). EXE.
SQL Server DATEDIFF() Function The DATEDIFF() function returns the difference between two dates.
A more elegant solution is to use DateTime and DateInterval.
<?php
// @link http://www.php.net/manual/en/class.datetime.php
$d1 = new DateTime('2011-09-01');
$d2 = new DateTime('2012-06-06');
// @link http://www.php.net/manual/en/class.dateinterval.php
$interval = $d2->diff($d1);
$interval->format('%m months');
Have a look at date_diff
:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%m months');
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With