Jump to content

Recommended Posts

I need to come up with something that will take the current month, for example this month is april.

It would create a drop down list like

 

Feb 1 - feb 30

march 1 - march 30

April 1 - April 30

Basically the last 3 months, including this one.

Then I am going to need to save the month as the value, so I can pass it to the script so I know what date ranges I am looking for.

I can't think of a logical way to do this, and still account for the fact that some months have 29 days, some have 30, and some have 31.

Link to comment
https://forums.phpfreaks.com/topic/48630-solved-last-3-months-dropdown/
Share on other sites

that should suffice...

<?
function switch_month($m){
while($m{0}=="0") $m=substr($m,1);
$m=strtolower($m);
switch($m){
  case "1": return "january"; break;
  case "2": return "february"; break;
  case "3": return "march"; break;
  case "4": return "april"; break;
  case "5": return "may"; break;
  case "6": return "june"; break;
  case "7": return "july"; break;
  case "8": return "august"; break;
  case "9": return "september"; break;
  case "10": return "october"; break;
  case "11": return "november"; break;
  case "12": return "december"; break;
  case "january": return "01"; break;
  case "february": return "02"; break;
  case "march": return "03"; break;
  case "april": return "04"; break;
  case "may": return "05"; break;
  case "june": return "06"; break;
  case "july": return "07"; break;
  case "august": return "08"; break;
  case "september": return "09"; break;
  case "october": return "10"; break;
  case "november": return "11"; break;
  case "december": return "12"; break;
}
}

echo '<select>';
for($i=date(m);$i<=date(m)-3;$i--){
echo '<option>'.switch_month($i).'<option>';
}
echo '</select>';

?>

echo date("d M y", mktime(0, 0, 0, date("m", 1174831470) , date("d", 1174831470)-date("d", 1174831470)+1, date("Y", 1174831470)));

echo date("d M y", mktime(0, 0, 0, date("m", 1174831470)+1 , date("d", 1174831470)-date("d", 1174831470), date("Y", 1174831470))); 

echo date("d M y", mktime(0, 0, 0, date("m", 1163831470) , date("d", 1163831470)-date("d", 1163831470)+1, date("Y", 1163831470)));

echo date("d M y", mktime(0, 0, 0, date("m", 1163831470)+1 , date("d", 1163831470)-date("d", 1163831470), date("Y", 1163831470))); 

 

that should get you going... you can improve on that.

can you post the code you've tried already? c'mon lad, you know the rules...

I had nothing yet, I was brain storming.

My only idea was to save them all in an "array" get the current month and throw out the last 3.

However, I wasn't expecting all the working examples, this give's me something great to build off of, greatly appreciated.

I can definitely get something work now, I was originally just wanting a little direction, perfect.

Thanks.

<?php
// get array of months information
$months = array("Jan"  => "01-31",
			"Feb"  => "01-28",
			"Mar"  => "01-31",
			"Apr"  => "01-30",
			"May"  => "01-31",
			"Jun"  => "01-30",
			"Jul"  => "01-31",
			"Aug"  => "01-31",
			"Sep"  => "01-30",
			"Oct"  => "01-31",
			"Nov"  => "01-30",
			"Dec"  => "01-31");
// get months
$thismonth       = date();
$thismonth       = strtotime($thismonth);
$lastmonth       = strtotime("-1 month", $thismonth);
$monthbeforelast = strtotime("-2 month", $thismonth);
// turn them into the proper format
$thismonth 		 = date("M");
$lastmonth  	 = date("M", $lastmonth);
$monthbeforelast = date("M", $monthbeforelast);
echo $thismonth;
echo "<br />";
echo $lastmonth;
echo "<br />";
echo $monthbeforelast;
echo "<br />";
?>

<form name="dates" id="dates" action="" method="post">
<select name="months">
<option value="<?php echo $monthbeforelast; ?>"><?php echo $monthbeforelast . $months['$monthbeforelast']; ?></option>
<option value="<?php echo $lastmonth; ?>"><?php echo $lastmonth . $months['$lastmonth']; ?></option>
<option value="<?php echo $thismonth; ?>"><?php echo $thismonth . $months['$thismonth']; ?></option>
</select>
</form>
<?php
print_r($months);
?>

I want to be able to get this to work.  I created something now, that works perfectly except for 1 thing.

It's not reading my array right, here is the full output of what is above.  However where you see

echo $thismonth . $months['$thismonth']; it's ignoring me trying to call that month from the array to get the numbers.

 

Apr

Mar

Feb

Array ( [Jan] => 01-31 [Feb] => 01-28 [Mar] => 01-31 [Apr] => 01-30 [May] => 01-31 [Jun] => 01-30 [Jul] => 01-31 [Aug] => 01-31 [sep] => 01-30 [Oct] => 01-31 [Nov] => 01-30 [Dec] => 01-31 )

Ok, that is the outputs the drop down menu (properly), I just need to check 2 things.

1. WHy isn't it outputting those number ranges and

2. Are those number ranges/dates correct, (I got them from wikipedia)

3. Using the M format is it going to match those months listed in the array or some of those abbreviations wrong, thanks.

 

Ok it's because I had '$variable' in the array I forgot to remove the single quotes.

Ok just wanted to make sure, based on the output you get from

date("M");// uppercase M

is the following list correct

$months = array("Jan"  => "01-31",

"Feb"  => "01-28",

"Mar"  => "01-31",

"Apr"  => "01-30",

"May"  => "01-31",

"Jun"  => "01-30",

"Jul"  => "01-31",

"Aug"  => "01-31",

"Sep"  => "01-30",

"Oct"  => "01-31",

"Nov"  => "01-30",

"Dec"  => "01-31");

I know some of the abbreviates are correct like Dec, and NOV adn OCT but I am not sure what it sends out for june or july, and some of the others.

Also I know all of these ranges are correct, but there was something on wikipedia,

http://en.wikipedia.org/wiki/Months

  1. January, 31 days

  2. February, 28 days, 29 in leap years, or 30 on certain occasions in related calendars

  3. March, 31 days

  4. April, 30 days

  5. May, 31 days

  6. June, 30 days

  7. July, 31 days

  8. August, 31 days

  9. September, 30 days

  10. October, 31 days

  11. November, 30 days

  12. December, 31 days

On the one for febuary, this is a little disturbing.

I hope it's fine I just don't want to launch this a few years from now a major error that's going to cause problems (multiple) throughout the entire system.I also have a few more features to build into this, I just want to get this part right first.

Wow... lots of suggestions here. Try something a little more consolidated:

<select name="month">
<?php
list($m, $y) = explode('-', date('m-Y'));
for ($i = -3; $i < 0; $i++) {
  $ts = mktime(0,0,0,$m+$i,1,$y);
  $n  = date('F', $ts);
  $days = date('t', $ts);
  echo "<option value=\"$ts\">$n 1 - $days</option>\n";
}
?>
</select>

 

Notice that using mktime() to come up with the timestamp will come up with all your calculations for you. You don't need to worry about year rollovers or leap years.

Well it outputted Jan/feb/march, and it had the proper "dates" with it, but it needs to output this months, last months, and the months' before.  That's why I was trying to do it this other way, I am not very familiar with this syntax

<?php
list($m, $y) = explode('-', date('m-Y'));
for ($i = -3; $i < 0; $i++) {
  $ts = mktime(0,0,0,$m+$i,1,$y);
  $n  = date('F', $ts);
  $days = date('t', $ts);
  echo "<option value=\"$ts\">$n 1 - $days</option>\n";
}
?>

That's why a lot of times I come up with something else based on the general "suggestions" that are sometimes offered.

See above, I know what list does, it takes those 2 variables and fills them with 2 variables from an array you specify (can't really explain it's functionality but I know what it does).

Then when you get with all this

for ($i = -3; $i < 0; $i++) { to me this is just saying

i = minus 3

then i also is less than 0, and you increment i

I am not sure about all this syntax, and when I look up the stuff for the for loop I don't fully get it, that's why I never took the time to use a for loop, it confused me more than helped me.

That is why I looked at the suggestions and created something different so I could fully understand everything I was looking for, because I also have to make sure my back end script knows the current year (which I can do in that script), but I may have alterations I have to make based on requests from the client.

Like how the 3 months are going to display and other stuff.

SO far I tried what you said directly, and it worked, but it only outputted the first 3 months in the drop down menu.

The only other problem is, if I had to make alterations I have

<?php
list($m, $y) = explode('-', date('m-Y'));
for ($i = -3; $i < 0; $i++) {
  $ts = mktime(0,0,0,$m+$i,1,$y);
  $n  = date('F', $ts);
  $days = date('t', $ts);
  echo "<option value=\"$ts\">$n 1 - $days</option>\n";
}
?>

I don't understand how a for loop works, I don't understand what that means where you outputted the option lists, I don't understand what F, t are in the formats, and I really have no standing experience with mktime (where I have used strtotime, strftime, and date quite a bit.)

Ah, ok that's the reason it didn't work, I didn't specify enough.

I meant last 3 months (including this one.)

So it would be February, March, April

basically that's what I meant.

Sorry about that, I can modify it myself, but could you break down the code so I understand it better, this looks like a much simpler solution to get the same results, with less code.

could you break down the code so I understand it better, this looks like a much simpler solution to get the same results, with less code.

 

Here's the breakdown:

<?php
// Get the month and year of the current month
list($m, $y) = explode('-', date('m-Y'));

// if you're wanting the previous three months, set up a loop to start with
// three months ago (-3) and to up through last month (-1). If you want to 
// include the current month, simply change the "$i < 0" to "$i < 1"
for ($i = -3; $i < 0; $i++) {
  $ts = mktime(0,0,0,$m+$i,1,$y); // Get the timestamp of the calculated month
  $n  = date('F', $ts); // Get the month name
  $days = date('t', $ts); // Get the total number of days in the month
  echo "<option value=\"$ts\">$n 1 - $days</option>\n"; // print out your option
}
?>

 

Make more sense now?

Ah perfect, after looking that your explanation then reviewing the for statement over at w3schools I have a better understanding of it.

uh, well I understand what your script is doing, but I am still lost as to what the for loop does.

THe reason being, I see people using for, and I alway use while/foreach.

Which is better, what are the best situations in which the for loop is needed.

Which is better, what are the best situations in which the for loop is needed.

 

foreach() is usable to iterate through arrays. Since we're not looking at using an array here, that leaves you with for or while. In this case, for is much more consolidated than while would be. IMO, if you know exactly what the values are you are wanting to iterate (-3 to -1 in our case), for() is the loop of choice. If you are waiting for a value to become true (or false) without knowing how long that will take, most likely while() would be the better solution.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.