?
Solved

Month Select Box Dynamic Range

Posted on 2014-01-07
5
Medium Priority
?
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

719 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