Forum Moderators: coopster

Message Too Old, No Replies

Convert month name to month number

using a variable - not the actual name

         

doodlebee

6:00 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



I'm currently working on a script, and I've come to a standstill on one point.

To this point, the script will fetch a certain key that's located in a database. This key is in a month/year format (ex: September 2007). I've gotten it to the point where the script pulls it from the database and splits the value into month and year.

However, now I need the month to be converted to a numerical value - but since the month is an unknown (and called by the script I'm writing), I can't quite make it convert to the proper number.

Right now, I have this:


list($meta_month, $meta_year) = split('[ ]', $meta_value);
$y = $meta_year;
$m = date("n",mktime(0,0,0,$meta_month));

But the output is *always* "12", no matter what I do. I've also tried this:


list($meta_month, $meta_year) = split('[ ]', $meta_value);
$y = $meta_year;
$monthname = strtotime('$meta_month');
$m = date("n", $monthname);

but the output is usually "-1" - which tells me that something isn't going right.

Would anyone know how to convert the month name to the correct value? (September = 9, July = 7, and so on...) I just can't figure out what I'm doing wrong here.

Thanks :)

lorax

7:00 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is one of the reasons I store all dates as UNIX timestamps. Then you can manipulate the stored data and use it or output it in any format you wish.

doodlebee

7:45 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



That might be a good idea, however, the date on question is end-user input. Basically, I have a form field that is optional. If the end user chooses to use it, then the field (the key) will have the value (end user input) placed into the database. The end user can put in anything they want - but what it's supposed to be used for is to put in the month and year they want.

I don't think I can convert that into a UNIX timestamp, since the value can be *any* date (in fact, it could be anything at all - but I'm looking for a date), in the past or future. My script simply looks for those values so they can be grouped together. I need it - at this time - to be input as "September 2007" (for example) so I can use this data for other things within the script and generated output.

So, it's all well and good to have it as a UNIX timestamp, but that's not the purpose of this script. I just need to know how to turn the end-user input of "September" into "9".

Anyone have any ideas for that?

If not, would it be easier to have them input "9/2007", and then convert the "9" to "September" later on?

russkern

8:29 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



I know this may be kind of long and I didn't bother to work it out entirely but...

Explode off your year, and then use a series of if/else if statements to establish your Month values...

if ($month="January") {
$m="1";
} else if ($month="February") {
$m="2";

etc....

I could probably work it out further if necessary, but your code looks like you'll figure out my direction quickly enough...

Russ

mooger35

8:59 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



You can try this...

$month = "July"; //or whatever
$month_number = "";

for($i=1;$i<=12;$i++){
if(date("F", mktime(0, 0, 0, $i, 1, 0)) == $month){
$month_number = $i;
break;
}
}

echo $month_number;

cameraman

9:05 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your second method will work, but you have a problem here:
$monthname = strtotime('$meta_month');

Remove the single quotes - singles don't evaluate the contents as a variable, so you're trying to convert the literal text dollar-sign meta_month. You don't need quotes at all in this case.

I don't think I can convert that into a UNIX timestamp, since the value can be *any* date

Sure you can, that's what strtotime is doing for you. It will do 'September 1970' or 'October 2025' or '3 weeks ago'.

You'll probably run into problems in the future trying to evaluate free-form inputted text as something meaningful. In your example, if I put in Sepptember 2007 it's all blown. A more structured method would be better, but you should, at the least, check it at input time so you can tell the user to correct it if it isn't in the format you'll need later.

cameraman

9:09 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



russkern and mooger35, how about this instead:
$monthnames = array('January','February','March','April'....

$month = "July";
$monthnumber = array_search($month,$monthnames);

If you want January to be 1, either add one to $monthnumber or put a bogus entry as the first one in the array: $monthnames=array('nothing','January'...

mooger35

9:12 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



ya that works as well... I'm sure there are numerous ways to do this.

Having the the original data in the correct format would be the best method though so, as stated before, you can easily convert it to what you want.

Also... would be probably easier to do the array like:

$monthnames = array(1 => 'January','February','March','April'....);

Wouldn't have to add the 1 later.

doodlebee

11:22 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



Yes!

Thank you :)

This is what worked:


list($meta_month, $meta_year) = split('[ ]', $meta_value);
$y = $meta_year;
$m = $meta_month;
$ylink = "newsletter/" . $y;


$monthnames = 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');


for($i=1;$i<=12;$i++){
if(date("F", mktime(0, 0, 0, $i, 1, 0)) == $m){
$month_number = $i;
}
}

the $ylink and $m values are what's echoed in the URL I'm creating...awesome - thanks to you all :)