Translate date("d F Y (H:i) function php

31,218

Solution 1

WordPress has date_i18n to retrieve the date in localized format, based on timestamp.

Try:

echo date_i18n("d F Y (H:i)", $timestamp);

WordPress has an extensive page on how to format date and time.

Solution 2

For the french language I use this

setlocale(LC_ALL, 'fra');

echo strftime("%A %d %B %Y",time()); 

For in portuguese

setlocale(LC_ALL, 'ptg');  //

echo strftime("%A %d %B %Y",time());

see Language Strings Country/Region Strings.

Solution 3

The documentation for date already answers this:

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

And strftime says that the way to do what is by using setlocale:

Format the time and/or date according to locale settings. Month and weekday names and other language-dependent strings respect the current locale set with setlocale().

That said, the C locale-aware functions do not provide sufficient functionality for languages that have cases. In such situations (i.e. most of the time) you need to roll your own.

Share:
31,218
Lucas Bustamante
Author by

Lucas Bustamante

Updated on July 15, 2022

Comments

  • Lucas Bustamante
    Lucas Bustamante almost 2 years

    I'm brazilian and there's a wordpress plugin that uses

    " . date("d F Y (H:i)",$date) . "
    

    Output: 16 January 2013 (00:54)

    But it should be 16 Janeiro 2013 (00:54), in portuguese... How can I change it?

    PS: I think maybe the date is set by an external file provided by the plugin creator :p I'm not sure though

  • Lucas Bustamante
    Lucas Bustamante about 11 years
    I tried to add <?php setlocale(LC_ALL, “pt_BR”, “ptb”); ?> to the header.php, inside <head> tags, but it didn't work, also inside the plugin file and stuff
  • Waihon Yew
    Waihon Yew about 11 years
    @user2056484: Locale names are dependent on your server's OS. There is no guarantee that these particular locales are installed on your own. In general, setlocale is insufficient for any serious work and you will probably have to find another solution.
  • Lucas Bustamante
    Lucas Bustamante about 11 years
    Hmm, What do you suggest @Jon? I'm trying in a lot of ways here :p Here's the script's pastebin: pastebin.com/CZpfBQpF -- Plus: I've tried to translate locale.php, no success :/
  • Waihon Yew
    Waihon Yew about 11 years
    @user2056484: I suggest finding a suitable implementation online (most self-respecting PHP frameworks should have one) or writing your own.
  • Lucas Bustamante
    Lucas Bustamante about 11 years
    Jon, i'm noob at php...I'm not lazy, but I have 30 items on my check-list to fix and present the client the website, and I don't know what to do :p I think i'll skip this :/
  • Lucas Bustamante
    Lucas Bustamante about 11 years
    yayyy!! It works like a charm!! Great solution, simple and effective! Thanks a LOT man!! Saved 2 days of work!!!!!!
  • dominicbri7
    dominicbri7 almost 11 years
    I tried every possible way to do this with native PHP and for some reason it just would not work.. but with this WordPress function it works perfectly as expected!
  • RRikesh
    RRikesh almost 11 years
    I'm glad it helped you! The guys working on WP are doing an awesome job :)
  • Gino
    Gino over 7 years
    I have a plugin who show date from RSS fetch, but nothing work to translate it. the plugin use this forma: $item->get_date('F j, Y - g:i a');
  • ValRob
    ValRob about 5 years
    wtf: 01 enero 1970 (00:00)
  • RRikesh
    RRikesh about 5 years
    @ValRob That's unix time zero. is your $date correct?