Forum Moderators: coopster
So, I need to create a function that will output months and years in a loop. (I swear I had this - I'm so mad at myself for losing it!)
Basically, I need to just display months and years from say, 2000 to present (no matter when the "present" may be - ten years from now this code should still work...well, all PHP upgrading aside LOL) in descending order. It's not going to be extracted from a database, though - I just need to formulate this function to I can compare information to it. So, say I have something in my database that's dated for "June 2003", then I want to compare the "June 2003" to the month/year function and show the output for anythign in the database that has "June 2003".
Anyway, I think I said more than i needed to. I can get the *latter* part to work just fine (in fact, it's already working - I can't get *this* current month/year to do what I want - my issue is in getting *past* ones to do it) - and I just need to figure out some code to have a list of month/year to compare with.
If it helps to see my current-and-functioning-perfectly code that shows the *current* month (maybe it'll help to give an idea of what I'm trying to do):
<?php
// get current month and year
$curr_month = date('F');
$curr_year = date('Y');
$curr_date = $curr_month . " " . $curr_year;
//database query here...
if ($pageposts) : foreach ($pageposts as $post) :
$post_id = $post->ID;
$key = "newsletter_month_year";
$single = "true";
$month_ID = get_post_meta($post_id, $key, $single);
$news = str_replace(' ', '_', $month_ID);
// compare $month_ID to the current date
if ($month_ID == $curr_date) {
So what I'm trying to do is create a variable that will list the $past_date to compare $month_ID to.
Would anyone know how to list the months/years so I can accomplish this? I hope I'm making sense!
<?
$startyr = 2001;
$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
// get current month and year
$curr_month = date('F');
$curr_year = date('Y');
$curr_date = $curr_month . " " . $curr_year;
$old_date='';
$counter = 0;
while (strcmp($old_date,$curr_date)) {
$old_date = $months[$counter] . ' ' . $startyr;
echo '<br>',$old_date;
$counter++;
if ($counter == 12) {
$counter = 0;
$startyr++;
}
}
?>
though this just echo's them in ascending order, you could maybe read them into an array and flip it, then output the contents
is that what you mean?
I was actually going to come in today and post my "resolution" (even though it's only partial to what I'm doing).
I did figure out how to echo out the month/year issue, and what I did works to display a list of months/years from the year 2000 to present:
for($y=2000;$y<=date('Y');$y++) { //start in the year 2000
$m = array(1 => 'January',2 => 'February',3 => 'March',4 => 'April',5 => 'May',6 => 'June',7 => 'July',8 => 'August',9 => 'September',10 => 'October',11 => 'November',12 => 'December');
foreach($m as $month) {
echo $month;
}
}
That works very well for the initial issue I asked about...but now I've discovered further issues with my overall project. (but that's another thread!)
AGain, thanks a bunch - this forum is terrific :)
for($y=2000;$y<=date('Y');$y++) { //start in the year 2000
$m = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');
while(list($key, $month) = each($m)) {
// prepend single digit numbers with a leading "0"
if ($key<=9) {
$dbkey = "0" . $key;
} else {
$dbkey = $key;
}
} Don't know if that'll help someone, but there you go :)