Date formatting with qTranslate


Almost a year ago I described here how I had managed to set up two different date formats for German and English in my WordPress theme. That’s why especially users of the plugin “qTranslate” ended up here frequently, because I had found a – rather muddled, but working – solution to show each language in its own format, since the method recommended by the author didn’t work for me then. Eleven months later however, the plugin has developed quite a bit and offers four options for date formatting.

I will show you how I proceeded for this site. First I checked the box “Use emulated date function” in the advanced qTranslate settings. That means that you can now use the_date() in your templates as usual, but qTranslate will automatically convert things like weekdays or month names to the current language. You do not have to use the qTranslate function qtrans_formatPostDateTime() as it used to be! This setting seems the most comfortable to me.

A plain date display without additional text (like “Sunday, 19th April 2009”) could look like this:

the_date('l, jS F Y');

For this site, I wanted it to be displayed like “19. Apr 2009”. That is done as follows:

the_date('j. M Y');

After that, I added a line break between those three:

the_date('j.MY');

The “r” must be protected by a backslash, because it is already reserved by the date() function. You generally have to mind if the additional text in your date format contains letters that are being recognized by the function as formatting parameters. They all have to be escaped. Example:

the_date('l, jS F Y \a\t g:i a');

In such a case you have to escape all the letters of the word “at”, because otherwise they would be interpreted by the date() function and produce a mess:

Sunday, 2nd November 2008 am 30 7:04 am

Now, if you would like to have the words “at” and “am”/“pm” in your English date, but “um” and “Uhr” in the German one, you can simply specify two different formats thanks to the (relatively) new qTranslate quicktags:

the_date('[:en]l, jS F Y \a\t g:i a[:de]l, j. F Y \u\m G:i \U\h\r');

This example would output something like “Sunday, 19th April 2009 at 4:18 am” in English and “Sonntag, 19. April 2009 um 4:18 Uhr” in German.

A list of all parameters of the date() function can be found on php.net.

P.S.: Accordingly, the old qTranslate function qtrans_formatCommentDateTime() isn’t needed anymore either. You can use the standard WordPress function comment_date() instead, e. g.:

comment_date('[:de]j. F Y[:en]jS F, Y');

40 responses to “Date formatting with qTranslate”

  1. Prima, eine Seite mit Tipps für qTranslate. Gerade hab ich das richtige Datum selbst mit Trial&Error im Template und qTranslate für die neue Website hingebügelt. Mir kommen die Parameter schon zu den Ohren heraus….!

  2. I don’t see any problem? I click on English, the page is in English, click on French, the page is in French, click German, the page is German.
    So you mean the stuff like “Welcome to Baby Sitter Registration” or “Enter E-mail and Username”? That is not inside the default .mo files, of course. You can either enter that in your template manually like _e('[:en]English text[:de]German text[:fr]French text'); or add the sentences to your .mo files.

  3. i’ve got the same problems and for a reason local it is still working, but not on my webserver. so i had to find another solution to get my dates multilingual.

    here is what worked for me and what is used by wordpress itself:

    <?php echo date_i18n( "d. F Y", strtotime(get_the_time("Y-m-d")) ); ?>

    You can also realize different formats, by checking what get_locale() gives you back.

  4. hi qtranslate plugin does not work in my theme in local wamp server on my system and how we can add hindi language in this plugin
    pleaase help me .
    thisplugin change backend content in different language but pronf end it is not work properly.

  5. Well, you have to create a new language in your WordPress backend under “Languages”, for example:

    Language code: hi
    Flag: in.png
    Locale: hi_IN
    

    Also, you have to download a Hindi .mo file and put it inside your wp-content/languages folder.
    You can get one here.

  6. Hi, thanks for the tips! I’ve got my site running WordPress 3.2.1 and qtranslate 2.5.24. The dates show fine in both English and German using just:

    <?php the_time('j F Y') ?>

    . June will display as Juni in the German version, and October as Oktober. Perfect. BUT… come to March, and it shows as M?rch… as if it cannot deal with characters with an umlaut, like ä.

    Any ideas? Thanks in advance.

  7. Hmmm, it’s a problem with the encoding. It shouldn’t be happening, if everything (server, database, WordPress) is set to UTF-8. So maybe one of them isn’t?

    Anyway, you could try to wrap it in utf8_encode(), like this:
    <?php utf8_encode(the_time('j F Y')) ?>
    Maybe that will help. (The corresponding command for decoding is utf8_decode(), but I think you need ENcode.) However, no guarantees that it will work. ;)

Leave a Reply

Your email address will not be published. Required fields are marked *