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. Hi Ginchen

    i had made one php script in template directory.now i had created one “page” in admin side called “Products”.when user click on the “products” link, i have to call that upper php script.

    do u know how can i do that?

  2. You could simply choose your PHP script as the template for your “Products” page. The file should begin with something like this:

    
    

    Then you can edit the page in the WordPress backend and choose “Products” as template for it.

  3. Hi Ginchen

    http://www.screencast.com/users/Tatva13/folders/Default/media/1d4c73fa-ec5f-49ff-be9c-ae25d14458b8

    i had attached a link to screen shot of product page in admin side.should u explain how wordpress executed this page? wp-e-commerce plugin created this page when i activated it.

    what does mean [productpage]?

    when i run that page on front side,”product_page.php” script is executed.that script is location like below

    “wp-content\plugins\wp-e-commerce\themes\default\products_page.php”

  4. Well, the e-Commerce plugin needs a certain page where it can display the products. That’s your products page. The plugin creates a so-called “shortcode”, [productspage], that you can place wherever you want in order to display your products. This is probably done in one of the main plugin files, like wp-ecommerce.php or whatever.

    However, as I said, I have never used that plugin, so I don’t know anything about how to use it or how it works exactly. They have a very detailed website, you should rather ask there. Or in their forum. Or have a look at the documentation. For example, this might be what you’re looking for.

  5. I can’t seem to get this to work on my website.
    If you have time would you like to help me with this?
    Dates/times keep getting mixed up, no matter what setting I use in qTranslate.

Leave a Reply

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