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

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.
$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...

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')

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()
function mdate($datestr = '', $time = '')
        if ($datestr == '')
            return '';
        if ($time == '')
            $time = now();
        if ($datestr == 'F'){
                    switch (date($datestr, $time)){
                        case 'January' :
                            return 'janeiro';
                        case 'February' :
                            return 'fevereiro';
                        case 'March' :
                            return 'março';
                        case 'April' :
                            return 'abril';
                        case 'May' :
                            return 'maio';
                        case 'June' :
                            return 'junho';
                        case 'July' :
                            return 'julho';
                        case 'August' :
                            return 'agosto';
                        case 'September' :
                            return 'setembro';
                        case 'October' :
                            return 'outubro';
                        case 'November' :
                            return 'novembro';
                        case 'December' :
                            return 'dezembro';

        $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.

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.