| |
Welcome to the dBforums forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact contact support.
If you prefer not to see double-underlined words and corresponding ads, place your cursor here for ContentLink opt out.
|
 |

05-05-07, 13:33
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 82
|
|
|
Months between two dates
|
Hey guys,
How can I take two dates (say 2005-12-29 and 2007-01-17)... and echo all the months between these dates?
So:
Code:
January 2007
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
December 2005
If you get me?
Thanks,
Jordan
|
|

05-05-07, 17:16
|
|
Jaded Developer
|
|
Join Date: Nov 2004
Location: out on a limb
Posts: 5,459
|
|
..well I suppose you could write a function which returns the months as required
it could return the values as an array if you wanted to use the returned values elsewhere in the script
__________________
Warning
May! contain traces of NUT. people with NUT allergies should not pay attention to any of the above
|
|

05-06-07, 07:44
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 82
|
|
|
Ok... so I used the code:
PHP Code:
function get_months($date1, $date2) {
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$my = date('mY', $time2);
$months = array(date('F', $time1));
$f = '';
while($time1 < $time2) {
$time1 = strtotime((date('Y-m-d', $time1).' +15days'));
if(date('F', $time1) != $f) {
$f = date('F', $time1);
if(date('mY', $time1) != $my && ($time1 < $time2))
$months[] = date('F', $time1);
}
}
$months[] = date('F', $time2);
return $months;
}
print("<pre>");
print_r(get_months('2005-02-20', '2005-12-31'));
print("</pre>");
Which echos:
Code:
Array
(
[0] => February
[1] => March
[2] => April
[3] => May
[4] => June
[5] => July
[6] => August
[7] => September
[8] => October
[9] => November
[10] => December
)
How can I get rid of everything but the actual months?
|
|

05-06-07, 12:52
|
|
Registered User
|
|
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
|
|
What do you mean "get rid of everything but the actual months"? The data is in whatever form your code was written to cause it to be in.
Since you place the information into an array and return that array from the function, you have an array. You can then use a foreach(...) loop to iterate through each element in the array or instead of placing the information into an array, concatenate it into a string with the formatting you want in the function and return that string from the function.
|
|

05-08-07, 14:47
|
|
Registered User
|
|
Join Date: May 2007
Posts: 4
|
|
This should work...
PHP Code:
$data = get_months('2005-02-20', '2005-12-31');
for($i=0;$i<count($data);$i++){ $month = $data[$i]; echo "$month<br>"; }
|
|

05-09-07, 10:56
|
|
Registered User
|
|
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
|
|
If you want to make it even shorter :
Code:
$data = get_months('2005-02-20', '2005-12-31');
for($i=0;$i<count($data);$i++){
echo $data[$i]."<br>";
}
You don't even need the variable assignment. What a waste of memory! 
|
|

05-09-07, 14:05
|
|
Registered User
|
|
Join Date: May 2007
Posts: 4
|
|
yeah every little helps 
|
|

05-10-07, 00:58
|
|
Registered User
|
|
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
|
|
Shortest (tested) -
PHP Code:
foreach(get_months('2005-02-20', '2005-12-31') as $value){
echo "$value<br />";
}
|
Last edited by dbmab : 05-10-07 at 01:01.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|