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. Hmm, only in IE 7 and nowhere else? – You have to know that IE 7 is one hell of a mess of a browser. It’s a bunch of crap, absolutely unreliable and totally buggy. So I shouldn’t wonder if it was just some kind of IE 7 bug…

    Do you have an “&” in the query string? I had a problem with that once. I had to encode the “&”, so the link would look like this: ?a=b&lang=de (instead of ?a=b&lang=de)
    Apart from that, I have no ideas, sorry… :(
    However, once more: If it only happens in IE 7, I would blame it on that crappy piece of software!

  2. Hi,Thanks

    one thing i notice that when application runs in other browser(FF 3,IE8),it runs perfectally.but title of that page like “Page not found

  3. I think, your problem with the “Page not found” thing is because you append the “?lang=…” directly to http://www.ma-nany.com/ (without index.php). It seems that qTranslate cannot “find” the page title then. Maybe you can use index.php as start page by default. That could be easily done with a .htaccess file.

  4. Hi Ginchen

    You are doing your language translate using Qtranslate. Am i right?

    if yes then when you have need to add any content in coding then u have to must add that content in all .mo language file.

    then how you are update your .mo files?

    are you using poedit for that or something else?

  5. No, I don’t edit the .mo files. Since I only have two languages (German and English), and I only need very few multilingual content in my template, I simply do it like this (in the template):
    <?php _e(‘[:de]Deutscher Text[:en]English Text’); ?>
    But in your case, where you have three languages and supposedly need many many more translations inside your template files, I’d rather use the .mo files, too. I use Poedit for this. As soon as you save your .po file, it will automatically create a .mo file, too.

  6. Hi,

    Thanks for feedback.i am updating my .mo files each time when i have to add new content in my template.but your follow code works like magic for me.

    <?php _e(‘[:de]Deutscher Text[:en]English Text‘); ?>

    you are mind blowing in wordpress! how long time r u worked on wordpress?please give me any useful material for wordpress learning.i am just beginner of it.

    so which one i use:follow your upper code or update .mo files each time?

  7. Hehe, I’ve been working with WordPress since February 2008 now (since I started this blog). However, I didn’t use any tutorials – whenever I had a problem, I just asked Google or http://codex.wordpress.org/ which is full of information. ;)

    In your case, I think I would stick with editing the .mo files. The example with _e(‘…’) is good if you have only 1-2 things to translate, but I think in your case it’s much more, so I’d say the .mo files are the better way.

  8. Hi Ginchen,

    i am using date-time function in my custom script,i have to print current month and year in this format (Jul 2009)..

    so when i am using below code i am not getting same result as u get in your above blog

    $month_name=date(“[:en]M [:fr]M [:de]M Y”,mktime(1,1,1,07,1,2009));

    any idea?

  9. This cannot be done with qTranslate language tags, because qTranslate cannot influence the output of the native PHP function date(), but only of the WordPress function the_date().

    However, qTranslate does set the locale in your environment variables to the currently chosen language, so you can simply use strftime(). Try like this:

    <?php echo strftime(‘%b %Y’); ?>

Leave a Reply

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