Jump to content

Month difference


web2000

Recommended Posts

llewellyntd at gmail dot com

29-Apr-2005 08:50

Here is a very easy way to get the difference, in days, between two dates:

 

$days = (strtotime("2005-12-31") - strtotime(date("Y-m-d"))) / (60 * 60 * 24);

print $days;

 

Found in user comments at www.php.net/date

 

<?php
$date_start = "04/01/2005";
$date_end = "06/01/2005";
$months = ((strtotime($date_start) - strtotime($date_end))/(3600*24*30));
print $months;
?>

 

Untested, the only issue is the amount of days some months can have, this is assuming all months have 30 days.

Link to comment
https://forums.phpfreaks.com/topic/48832-month-difference/#findComment-239336
Share on other sites

<?php
$d1 = strtotime('2005-06-01');
$d2 = strtotime('2007-04-25');

$y1 = date('Y', $d1);
$y2 = date('Y', $d2);
$m1 = date('n', $d1);
$m2 = date('n', $d2);

$month_diff = ($y2 - $y1) * 12 + ($m2 - $m1);

echo $month_diff;        // 22

?>

Link to comment
https://forums.phpfreaks.com/topic/48832-month-difference/#findComment-239360
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.