Welcome Guest, Not a member yet? Register   Sign In
get array of months between 2 dates
#4

[eluser]xwero[/eluser]
My solution
Code:
function get_months($date1, $date2)
{

$date1 = date('Y-m',strtotime($date1));
$date2 = date('Y-m',strtotime($date2));

if($date1 < $date2)
{
   $past = $date1;
   $future = $date2;
}
else
{
   $past = $date2;
   $future = $date1;
}

$months = array();
for($i = $past; $past<=$future; $i++)
{
   $timestamp = strtotime($past.'-1');
   $months[] = date('F Y',$timestamp);
   $past = date('Y-m',strtotime('+1 month',$timestamp));  
}

return $months;
}
The benefit of my code is that the function doesn't care about which date is in the highest.

Another way you could code the function is to let the chronology the dates are added control the sorting of the months, ascending or descending.


Messages In This Thread
get array of months between 2 dates - by El Forum - 11-28-2008, 12:57 PM
get array of months between 2 dates - by El Forum - 11-28-2008, 01:51 PM
get array of months between 2 dates - by El Forum - 11-28-2008, 02:07 PM
get array of months between 2 dates - by El Forum - 11-28-2008, 03:33 PM



Theme © iAndrew 2016 - Forum software by © MyBB