Solved

Month Select Box Dynamic Range

Posted on 2014-01-07
5
355 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 110

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 110

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 110

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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