Converting 3 char month into numerical equavalent

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

Converting 3 char month into numerical equavalent

Post by RedRasper »

Hi all,

Wonder if someone could help me. I'm sure there must be a way to do this using the date/time etc functions, but i can't see how!

I have a 3 digit month (ie 'Nov') and want to turn it into its numberical version (ie 10).

Could anyone help me?

Many thanks
RedRasper
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

I'm not used to looking around so I normally make my own scripts out of my own Ideas till someone like m3rjak point me PHP's built in functions <_< lol. Anyhow, you can have an array of the months and use array_search() to find the key of the month.

-Nay
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

make an array with all the elements in order
then search the array for a match
ex:

Code: Select all

<?php
$months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$theday =  'Mar';
$numberday = array_search($theday, $months) +1;
echo $numberday;
?>
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

Post by RedRasper »

Thank you both!

Nice little idea, I has thought about something like that, but dismissed it as i thought i'd have to do a for loop to search the array! Didn't know about array search!

Cheers!

RedRasper
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

[php_man]strtotime[/php_man] is your friend. If you couple it with [php_man]date[/php_man] you'll be sorted.
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

Post by RedRasper »

Hi patrikG

Thank you for that wonderful function I never knew existed! Thats two for me today!

Its a bit annoying though as I have written quite a few routines myself to work out weeks etc! Ah well you live and learn!

Thank you again,

RedRasper
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Glad it helped.
Post Reply