{"id":1472,"date":"2009-04-20T04:20:14","date_gmt":"2009-04-20T02:20:14","guid":{"rendered":"http:\/\/blog.ginchen.de\/?p=1472"},"modified":"2011-09-01T18:20:18","modified_gmt":"2011-09-01T16:20:18","slug":"datumsformatierung-mit-qtranslate","status":"publish","type":"post","link":"http:\/\/blog.ginchen.de\/en\/2009\/04\/20\/datumsformatierung-mit-qtranslate\/","title":{"rendered":"Date formatting with qTranslate"},"content":{"rendered":"<p>Almost a year ago I described <a href=\"http:\/\/blog.ginchen.de\/en\/2008\/05\/11\/datumsformatierung-in-wordpress\/\">here<\/a> 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 \"<a rel=\"external\"  href=\"http:\/\/www.qianqin.de\/qtranslate\/\">qTranslate<\/a>\" 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.<\/p>\r\n\r\n<!--more-->\r\n\r\n<p>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 <code>the_date()<\/code> in your templates as usual, but qTranslate will automatically convert things like weekdays or month names to the current language. <ins datetime=\"2009-05-03T19:29:34+00:00\">You do <em>not<\/em> have to use the qTranslate function <code>qtrans_formatPostDateTime()<\/code> as it used to be!<\/ins> This setting seems the most comfortable to me.<\/p>\r\n\r\n<p>A plain date display without additional text (like \"Sunday,&nbsp;19th&nbsp;April&nbsp;2009\u201d) could look like this:\r\n\r\n<pre lang=\"php\">the_date('l, jS F Y');<\/pre>\r\n\r\n<p>For this site, I wanted it to be displayed like \"19.&nbsp;Apr&nbsp;2009\". That is done as follows:<\/p>\r\n\r\n<pre lang=\"php\">the_date('j. M Y');<\/pre>\r\n\r\n<p>After that, I added a line break between those three:<\/p>\r\n\r\n<pre lang=\"php\">the_date('j.<b\\r \/>M<b\\r \/>Y');<\/pre>\r\n\r\n<p>The \"r\" must be protected by a backslash, because it is already reserved by the <code>date()<\/code> 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:<\/p>\r\n\r\n<pre lang=\"php\">the_date('l, jS F Y \\a\\t g:i a');<\/pre>\r\n\r\n<p>In such a case you have to escape all the letters of the word \"at\", because otherwise they would be interpreted by the <code>date()<\/code> function and produce a mess:<\/p>\r\n\r\n<pre>Sunday, 2nd November 2008 am 30 7:04 am<\/pre>\r\n\r\n<p>Now, if you would like to have the words \"at\" and \"am\"\/\u201cpm\" 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 <a rel=\"external\"  href=\"http:\/\/www.qianqin.de\/qtranslate\/forum\/viewtopic.php?f=3&t=385\">quicktags<\/a>:<\/p>\r\n\r\n<pre>the_date('[&#0058;en]l, jS F Y \\a\\t g:i a[&#0058;de]l, j. F Y \\u\\m G:i \\U\\h\\r');<\/pre>\r\n\r\n<p>This example would output something like \"Sunday,&nbsp;19th&nbsp;April&nbsp;2009&nbsp;at&nbsp;4:18&nbsp;am\" in English and \"Sonntag,&nbsp;19.&nbsp;April&nbsp;2009&nbsp;um&nbsp;4:18&nbsp;Uhr\" in German.<\/p>\r\n\r\n<p>A list of all parameters of the <code>date()<\/code> function can be found on <a rel=\"external\"  href=\"http:\/\/www.php.net\/manual\/de\/function.date.php\">php.net<\/a>.<\/p>\r\n\r\n<ins datetime=\"2009-05-03T19:29:34+00:00\">\r\n<p>P.S.: Accordingly, the old qTranslate function <code>qtrans_formatCommentDateTime()<\/code> isn't needed anymore either. You can use the standard WordPress function <code>comment_date()<\/code> instead, e.&nbsp;g.:<\/p>\r\n\r\n<pre>comment_date('[&#0058;de]j. F Y[&#0058;en]jS F, Y');<\/pre>\r\n<\/ins>","protected":false},"excerpt":{"rendered":"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 [&hellip;]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[78,81,84,323,82,79,606],"_links":{"self":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/1472"}],"collection":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/comments?post=1472"}],"version-history":[{"count":5,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/1472\/revisions"}],"predecessor-version":[{"id":1495,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/1472\/revisions\/1495"}],"wp:attachment":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/media?parent=1472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/categories?post=1472"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/tags?post=1472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}