Page 1 of 1

Rename month numbers in letters

Posted: Sun Nov 29, 2009 11:24 am
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.

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 11:36 am
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.

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 11:47 am
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;

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 11:56 am
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.

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 12:02 pm
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.

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 12:10 pm
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;
?>

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 2:43 pm
by MicroBoy
thnx a lot :D, which one do you think that would execute faster, or both are same for the speed.

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 3:53 pm
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

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 5:18 pm
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.

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 7:12 pm
by MicroBoy
AlanG thnx a lot for your help and time :D

Re: Rename month numbers in letters

Posted: Sun Nov 29, 2009 7:48 pm
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. :)