Solved

Month Select Box Dynamic Range

Posted on 2014-01-07
5
353 Views
Last Modified: 2014-01-07
Looking to code a select box that will update every month beginning with the current month and going back twelve months.

For example, a select box for this month would have options for Jan '14 - Feb '13.
0
Comment
Question by:t3chguy
  • 3
  • 2
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39762962
While I set up an example for you, read this article.  It will show you everything you need to know about how to do this.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39763033
Here is an example: http://www.laprbass.com/RAY_temp_t3chguy.php

<?php // RAY_temp_t3chguy.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28332606.html#a39762962

// THE MONTHS
$now = date('Y-m-1');
$old = date('Y-m-1', strtotime("$now - 11 MONTHS"));
$new = date('Y-m-1', strtotime('TODAY'));

// GET THE DATE STRINGS
while ($old <= $new)
{
    $arr[$old] = date("M 'y", strtotime($old));
    $old       = date('Y-m-1', strtotime($old . ' + 1 MONTH'));
}

$arr = array_reverse($arr);

// CREATE THE SELECT OPTIONS
$out = '<select name="month">' . PHP_EOL;
foreach ($arr as $val => $txt)
{
    $out .= '<option value="' . $val . '">' . $txt . '</option>' . PHP_EOL;
}
$out .= '</select)' . PHP_EOL;

// SHOW WHAT WE MADE
echo '<pre>';
echo htmlentities($out);

Open in new window

HTH, ~Ray
0
 
LVL 58

Expert Comment

by:Gary
ID: 39763045
Or a simpler way

for($i=0;$i<12;$i++){
	echo "<option>".date("F Y",strtotime("-".$i." Months"))."</option>"; 
}

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39763056
@cathal: You might want to test that on several different starting dates.  The term "Month" is ambiguous.  I've found that PHP mostly gets it right when you add or subtract a "Month" during the first 28 days of any month.  After that, the wheels begin to fall off.  If you normalize the date to the first of the month it seems to be right (or at least intuitively correct) every time.

See Ambiguity of the term "Month" for an explanation along with a test script that shows what can happen.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39763076
Realised a glitch with my sample, here's a better method that doesn't burp on the 31st.

$tempDate= strtotime(date("Y")."-".date("F")."-15");

for($i=0;$i<12;$i++){
	echo "<option>".date("F Y",strtotime("-".$i." Months",$tempDate))."</option>"; 
}

Open in new window


@Ray
Yeah I realised that, new method above sets a starting date
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question