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

[eluser]new_igniter[/eluser]
Hello,
Does anyone have a php function for finding the array of months values based on two dates?

for example

Code:
function getMonths('2008-01-01','2008-05-01') {

some code...


}
Thanks!!!
#2

[eluser]xwero[/eluser]
Can you define array of months?

the simplest way i know would be to strtotime the dates if they are in that format and loop until the month in the future is reached.
#3

[eluser]new_igniter[/eluser]
I actually just found and used this:
http://www.dbforums.com/showthread.php?t=1617949

Thanks for your help
#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.




Theme © iAndrew 2016 - Forum software by © MyBB