• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A little problem with date_helper

#1
[eluser]Unknown[/eluser]
I'm starting with CI and I was looking for a way to display the name of month.
I've discovered the date_helper and when looking into http://ellislab.com/codeigniter/user-gui...elper.html I found the mdate(). It says that
Quote:This function is identical to PHPs date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.

But I tryed to get the month name and I got caught in this line of the mdate() method.
Code:
$datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));

The problem is that for date('F') I got February for mdate('F') I got F.
I don't know how much I can change this str_replace and maintain it working for all the other functions (that I probably haven't figured yet). I'm noobie to regular expressions as well...

In second place I have to translate the name of the month to Portuguese.

Thanks for reading and thinking about it...

#2
[eluser]moodsey211[/eluser]
The function date and mdate have different formats in its parameters. If date('F') gives you the full name of the current month, in mdate, you should have something like this: mdate('%M')

#3
[eluser]Unknown[/eluser]
Thank you, it was almost exactly what I needed.
I had used a similar approach ( little bit lammer )

If possible could you tell me where did you get the acceptables formats to use into mdate()?
something such as
%M = part of months name
%d = number of current day
and so on?

Follows the modification I did to the mdate()
Code:
function mdate($datestr = '', $time = '')
    {
        if ($datestr == '')
            return '';
    
        if ($time == '')
            $time = now();
        if ($datestr == 'F'){
                    switch (date($datestr, $time)){
                        case 'January' :
                            return 'janeiro';
                            break;
                        case 'February' :
                            return 'fevereiro';
                            break;
                        case 'March' :
                            return 'março';
                            break;
                        case 'April' :
                            return 'abril';
                            break;
                        case 'May' :
                            return 'maio';
                            break;
                        case 'June' :
                            return 'junho';
                            break;
                        case 'July' :
                            return 'julho';
                            break;
                        case 'August' :
                            return 'agosto';
                            break;
                        case 'September' :
                            return 'setembro';
                            break;
                        case 'October' :
                            return 'outubro';
                            break;
                        case 'November' :
                            return 'novembro';
                            break;
                        case 'December' :
                            return 'dezembro';
                            break;
                            }

                }
        $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
                return date($datestr, $time);
    }
And doing like this I'm going to get the next month if you're accessing before the 15th and the next next month if its after the 15th.

Code:
function periodo_mes($tipo = ''){
        (mdate('%d')>15) ? $m=2 : $m=1;
        if ($tipo == 'extenso'){
            $mes = mdate('F', mktime(0, 0, 0, mdate('%m')+$m, mdate('%d'), mdate('%Y')));
            return $mes;
        } else {
            return mdate('%m')+$m;
        }
    }
Portuguese notes
extenso = full name
mes = month
periodo = period (not the red one)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.