Jump to content

[SOLVED] converting numbers to months - not working


Recommended Posts

Hi all,

 

I've written a basic conversion script to change a numerical value entered into a form to a calendar month, ie - 05 = May.

 

Here's the code:

 

if ($_POST['month'] == 01) {
		$month = "January";
	} elseif ($_POST['month'] == 02) {
		$month = "February";
	} elseif ($_POST['month'] == 03) {
		$month = "March";
	} elseif ($_POST['month'] == 04) {
		$month = "April";
	} elseif ($_POST['month'] == 05) {
		$month = "May";
	} elseif ($_POST['month'] == 06) {
		$month = "June";
	} elseif ($_POST['month'] == 07) {
		$month = "July";
	} elseif ($_POST['month'] == 08) {
		$month = "August";
	} elseif ($_POST['month'] == 09) {
		$month = "September";
	} elseif ($_POST['month'] == 10) {
		$month = "October";
	} elseif ($_POST['month'] == 11) {
		$month = "November";
	} elseif ($_POST['month'] == 12) {
		$month = "December";
	}

	echo $month."<br />";

	$cat_title = $_POST['saletitle'];
	$cat_day = $_POST['day'];
	$cat_mon = $_POST['month'];
	$cat_year = $_POST['year'];
	$cat_salenum = $_POST['salenum'];

 

But for some really strange reason, it stops working for August, September and I can't for the life of me figure out why...  Any ideas?  All the code was cut'n'pasted so it should work consistently right?

Yeah, I don't see any errors either.

 

On a side note, it's much easier to read if you setup an array:

$months = array(
  1  => "January",
  2  => "February",
  3  => "March",
  4  => "April",
  5  => "May",
  6  => "June",
  7  => "July",
  8  => "August",
  9  => "September",
  10 => "October",
  11 => "November",
  12 => "December",
);
$month = $months[$_POST['month']];

The form field is like this:

 

<td><input type="text" maxlength="2" name="day" size="1"/> - <input type="text" maxlength="2" name="month" size="1" /> - <input type="text" maxlength="4" name="year" size="3" /> <?php echo SALE_add_date_details; ?></td>

And when the form is submitted, it sends to a function - addSale (), which has the original code I posted earlier.

 

I get what you mean with an array - I am forcing the end user to input 2 numbers, so August would always be 08 - would this be reflected in the array by doing:

 

08 => "August",

 

??

don't use leading zeros...you'll run into problems with that. cus if you don't put quotes around it, PHP will drop the leading zero and make it a number. made one small mod to force the users input to a number:

 

$months = array(
  1  => "January",
  2  => "February",
  3  => "March",
  4  => "April",
  5  => "May",
  6  => "June",
  7  => "July",
  8  => "August",
  9  => "September",
  10 => "October",
  11 => "November",
  12 => "December",
);
$month = $months[(int)$_POST['month']];

A number starting with a zero is treated as an octal, I heard. Shorter code:

 

<?php
$month = date('F', strtotime('2000-' . str_pad($_POST['month'], 2, '0', STR_PAD_LEFT) . '-15'));
?>

 

Edit: Actually even shorter using mktime():

 

<?php
$month = date('F', mktime(0, 0, 0, (int) $_POST['month'], 15));
?>

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.