Rename month numbers in letters

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
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Rename month numbers in letters

Post by MicroBoy »

I have the date in this format m/Y (example: 11/2009) I want that the part of the month 'm' to replace with the name of the month. I know that I can use str_replace() but then I can have problems because It can replace even the numbers at year. So is there any change to replace just the numbers before the '/' or any other chance.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Rename month numbers in letters

Post by AlanG »

Best to stick with what you know. :) Append a forward slash to the search query and then replace it.
e.g.

Code: Select all

 $date = '11/2009';
 
$search = array('01/', '02/', '03/', '04/', '05/', '06/', '07/', '08/', '09/', '10/', '11/', '12/');
$replace = array('Jan/', 'Feb/', 'Mar/', 'Apr/', 'May/', 'Jun/', 'Jul/', 'Aug/', 'Sep/', 'Oct/', 'Nov/', 'Dec/');
 
$date = str_replace($search, $replace, $date);
If your not happy with that, you can use regular expressions, or extract the date using explode or whatever.
Last edited by AlanG on Sun Nov 29, 2009 11:55 am, edited 1 time in total.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Rename month numbers in letters

Post by MicroBoy »

It looks nice but it's not working, it still display the date as 11/2009.

Code: Select all

$data = $row['dataere'];
 
$search = array('01/', '02/', '03/', '04/', '05/', '06/', '07/', '08/', '09/', '10/', '11/', '12/');
$replace = array('Janar/', 'Shkurt/', 'Mars/', 'Prill/', 'Maj/', 'Qershor/', 'Korrik/', 'Gusht/', 'Shtator/', 'Tetor/', 'Nëntor/', 'Dhjetor/');
str_replace($search, $replace, $data);
 
echo $data;
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Rename month numbers in letters

Post by AlanG »

Sorry my bad. The str_replace function returns the modified string. It's pass by value. :) You need to assign the date to the newly modified one. I have amended my above post.

My fault for not testing it.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Rename month numbers in letters

Post by MicroBoy »

Thnx a lot, if anyone has a better idea please post.

p.s. this one is working perfect, but maybe someone has another better idea.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Rename month numbers in letters

Post by AlanG »

Oh well if your looking for elegant coding, here's another way of doing it. The first method I posted is pretty iterative, but it is simple to understand.

Code: Select all

<?php
$months = array('Janar/', 'Shkurt/', 'Mars/', 'Prill/', 'Maj/', 'Qershor/', 'Korrik/', 'Gusht/', 'Shtator/', 'Tetor/', 'Nëntor/', 'Dhjetor/');
 
$data = $row['dataere'];
 
$dateElements = explode('/', $data);
 
$myMonth = reset($dateElements);
$myYear = end($dateElements);
 
$data = $months[$myMonth - 1] . '/' . $myYear;
?>
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Rename month numbers in letters

Post by MicroBoy »

thnx a lot :D, which one do you think that would execute faster, or both are same for the speed.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Rename month numbers in letters

Post by daedalus__ »

uh?

Code: Select all

date('M/Y', $timestamp);
store or convert dates to timestamps and format with the date() function?

http://php.net/manual/en/function.date.php
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Rename month numbers in letters

Post by AlanG »

MicroBoy wrote:thnx a lot :D, which one do you think that would execute faster, or both are same for the speed.
Meh, I wouldn't worry about micro-optimization. I'd opt for the second example. I'd encapsulate it in a function though, just to clean it up a little.

daedalus__:

Yeah I did consider using strtotime and then the date function to format it. At the time I was wondering if the lack of a day would matter and also does the strtotime function accept 'MM/YYYY' as a valid input? (I can't test it at the moment and didn't bother earlier) If not, then your still going to have to slice and dice it.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Rename month numbers in letters

Post by MicroBoy »

AlanG thnx a lot for your help and time :D
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Rename month numbers in letters

Post by AlanG »

MicroBoy wrote:AlanG thnx a lot for your help and time :D
No worries. :)

Just in the interest of furthering knowledge and all that other noble stuff. :)

I have looked into the strtotime issue. It won't accept a string '11/2009' as I suspected, but it does accept a string format of '01/11/2009'. The problem is it's the american version, so the '11' is the day. So to accommodate this you will still have to slice and dice the date string (making it '11/01/2009'. You could then drop the $months array and use the date function for formatting it, but as your not using english month names, it wouldn't suit your purpose.

I still think the best solution is the second one. :)
Post Reply