Month convert function?

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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Month convert function?

Post by spacebiscuit »

Hi,

Does anyone know if there is a function that given a month number (1-12) it will return the month name.

I could write my own case/select or function but wanted to know if there was an in-built function.

By the way foobar, I used the regex again today on another application, working well!

Thnaks,

Rob.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: Month convert function?

Post by foobar »

robburne wrote:
Does anyone know if there is a function that given a month number (1-12) it will return the month name.

I could write my own case/select or function but wanted to know if there was an in-built function.
Try this:

Code: Select all

$month = 5; //Let's say the months is May

echo date('m', mktime(0, 0, 0, $month, 0, 0));
robburne wrote:By the way foobar, I used the regex again today on another application, working well!
Glad to hear! :D
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

http://us3.php.net/date

read the entire page very very carefully. not being sarcastic,it can be tricky to find your answer.though i promise it's there.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Just had this problem yesterday. With strtotime you have to pass any value for a valid year in order for it to return the month. Anyone knows why?


This will also work

Code: Select all

$month = 5; //Let's say the months is May 

echo date('m', strtotime("$month 1970");
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

See? PHP is great. There are so many ways to do the same thing! :D
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Code: Select all

<?

$month = 5; 
echo date('m', strtotime("$month 1970"); 

?>
Parse error: syntax error, unexpected ';' in d:\Software\Apache\htdocs\skytrax\test.php on line 4

I tried replacing the quotations with an apostrophe but still getting an error.

Rob.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

coiunt how many left ( and how many right ) you have...if the two numbers are not equal you screwed up somewhere ;)
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Duh!

Thx :)
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

awww.... :oops:

Code: Select all

$month = 5; //Let's say the months is May 

echo date( 'm', strtotime("$month 1970") );
[/quote]
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Ok added the closing bracket and changed the year to 2005 but

Code: Select all

Warning: date() [function.date]: Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in
Rob.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try:

Code: Select all

$month = 5; //Let's say the months is May
echo date('M', strtotime("$month/01"));
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Seems stupid to have nested functions run just to receive the name of a month..

Code: Select all

<?

$months = array('Jan', 'Feb', 'Mar', 'etc.');

$month = $months[--$num];

?>
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Hi,

I found that this worked beter.....

$x=date('M',mktime(0,0,0,date(n),date(j),date(y)));

Thanks,

Rob.
Post Reply