Solved

Month Select Box Dynamic Range

Posted on 2014-01-07
5
350 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 108

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 108

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now