{"id":94,"date":"2008-05-11T12:41:35","date_gmt":"2008-05-11T10:41:35","guid":{"rendered":"http:\/\/blog.ginchen.de\/?p=94"},"modified":"2011-09-01T18:38:24","modified_gmt":"2011-09-01T16:38:24","slug":"datumsformatierung-in-wordpress","status":"publish","type":"post","link":"http:\/\/blog.ginchen.de\/en\/2008\/05\/11\/datumsformatierung-in-wordpress\/","title":{"rendered":"Date formatting in WordPress"},"content":{"rendered":"<ins datetime=\"2011-09-01T16:18:17+00:00\">\r\n<p><strong>Note:<\/strong> This post is quite old. Especially the suggestions under items 2 and 3 are no longer appropriate. More recent tips for date formatting with qTranslate are available <a href=\"http:\/\/blog.ginchen.de\/en\/2009\/04\/20\/datumsformatierung-mit-qtranslate\">here<\/a>.<\/p>\r\n<\/ins>\r\n\r\n<p>After updating to WordPress 2.5, I have &#8211; like many others, obviously &#8211; experienced the problem that post dates and times were not being localised anymore. Weekday and months names were not translated, and the number of comments was only indicated by an English &#8220;x comments&#8221;. The frequently recommended <code>setlocale()<\/code> function did not bring any improvement &#8211; and why should it? After all I had already set WordPress as well as PHP to German in the configuration files.<\/p>\r\n\r\n<p>I cannot tell the whys and wherefores, but after some trial and error I found solutions that work at least for me.<\/p>\r\n\r\n<!--more-->\r\n\r\n<h3>1.) Localised comments link<\/h3>\r\n\r\n<p>My template used the function <code>comments_popup_link()<\/code> to generate the link to the comments. In the past, this link was automatically translated into German, e.&nbsp;g. &#8220;Keine Kommentare&#8221; instead of &#8220;No comments&#8221;. As of version&nbsp;2.5, however, that doesn&#8217;t work anymore. So in my template I replaced the line<\/p>\r\n\r\n<pre>&lt;?php comments_popup_link() ?&gt;<\/pre>\r\n\r\n<p>by<\/p>\r\n\r\n<pre>&lt;a href=\"&lt;?php comments_link(); ?&gt;\"&gt;&lt;?php comments_number(); ?&gt;<\/a><\/pre>\r\n\r\n<p>In contrast to <code>comments_popup_link()<\/code>, the function <code>comments_number()<\/code> dutifully shows the number of comments &#8211; and the corresponding words like &#8220;keine&#8221;, &#8220;Kommentar&#8221; and &#8220;Kommentare&#8221; &#8211; in German. It does not generate the link to the comments though, so you have to enclose it in a link manually using <code>comments_link()<\/code>.<\/p>\r\n\r\n<h3>2.) Localised date display for posts<\/h3>\r\n\r\n<p>Instead of <code>the_date()<\/code> or <code>_e(the_date('l, d F Y, H:i','','',FALSE))<\/code>, I inserted the following into my template:<\/p>\r\n\r\n<pre>\r\n_e(\r\n    htmlentities(\r\n        strftime('%A, %e %B %Y at %I:%M %p',\r\n            strtotime(\r\n                the_date('Y\/m\/d H:i', '', '', FALSE)\r\n            )\r\n        )\r\n    )\r\n)\r\n<\/pre>\r\n\r\n<p>(Of course, you can simply put this on a single line &#8211; i just did it like that for the sake of legibility.)<\/p>\r\n\r\n<h5>What does this line of code do?<\/h5>\r\n\r\n<p>Roughly spoken, it causes the date not to be output directly by <code>the_date()<\/code>, but to be forwarded to <code>strftime()<\/code> for further formatting. For in contrast to <code>the_date()<\/code>, localisation works perfectly well when using <code>strftime()<\/code>.<\/p>\r\n\r\n<h5>For those who want to know exactly:<\/h5>\r\n\r\n<ol>\r\n<li>First the post date is being converted into the format &#8220;year\/month\/day&nbsp;hour:minute&#8221; using <code>the_date()<\/code>, e.&nbsp;g. <code>2008\/05\/11 10:30<\/code>.<\/li>\r\n<li>This date is then being forwarded to <code>strtotime()<\/code>, which converts it into a Unix timestamp, e.&nbsp;g. <code>1210494600<\/code>.<\/li>\r\n<li>And that timestamp is finally delivered to <code>strftime()<\/code>, with which you can format the date according to your regional settings as you like. Woohoo!<\/li>\r\n<li>Around it all I put a <code>htmlentities()<\/code> to prevent you from stumbling across a &#8220;M?rz&#8221; instead of &#8220;M&#228;rz&#8221;.<\/li>\r\n<\/ol>\r\n\r\n<p>The different formatting parameters for <code>strftime()<\/code> can be found <a rel=\"external\"  href=\"http:\/\/www.php.net\/manual\/en\/function.strftime.php\">on php.net<\/a>. The above example <code>'%A, %e %B %Y at %I:%M %p'<\/code> would output the following:\r\nSunday,&nbsp;11&nbsp;May&nbsp;2008&nbsp;at&nbsp;10:30&nbsp;am<\/p>\r\n\r\n<h3>Different localised date formats with qTranslate<\/h3>\r\n\r\n<p>If you run a multilingual site using the plugin <a rel=\"external\"  href=\"http:\/\/www.qianqin.de\/qtranslate\/\">qTranslate<\/a>, you can easily specify different date formats for the different languages by means of language tags. For example, I want the German date to be displayed in the format &#8220;Sonntag,&nbsp;11.&nbsp;Mai&nbsp;2008&nbsp;um&nbsp;10:30&nbsp;Uhr&#8221;, while the English one should read &#8220;Sunday,&nbsp;11&nbsp;May&nbsp;2008&nbsp;at&nbsp;10:30&nbsp;am&#8221;. You simply have to write the whole thing twice with the corresponding language tags and date formats. That will look as follows:<\/p>\r\n\r\n<pre>\r\n_e(\r\n    '&lt;!--:de--&gt;'.htmlentities(\r\n        strftime('%A, %e. %B %Y um %R Uhr', \r\n            strtotime(\r\n                the_date('Y\/m\/d H:i', '', '', FALSE)\r\n            )\r\n        )\r\n    )\r\n    .'&lt;!--:--&gt;&lt;!--:en--&gt;'.htmlentities(\r\n        strftime('%A, %e %B %Y at %I:%M %P', \r\n            strtotime(\r\n                the_date('Y\/m\/d H:i', '', '', FALSE)\r\n            )\r\n        )\r\n    ).'&lt;!--:--&gt;'\r\n)\r\n<\/pre>\r\n\r\n<p>qTranslate does provide its own function for post date formatting, which would abbreviate the whole thing:<\/p>\r\n\r\n<pre>\r\n_e(\r\n    '&lt;!--:de--&gt;'.\r\n    qtrans_formatPostDateTime('%A, %e. %B %Y um %R Uhr')\r\n    .'&lt;!--:--&gt;&lt;!--:en--&gt;'.\r\n    qtrans_formatPostDateTime('%A, %e %B %Y at %I:%M %P')\r\n    .'&lt;!--:--&gt;'\r\n)\r\n<\/pre>\r\n\r\n<p>But unfortunately it didn&#8217;t work for me &#8211; the result always looked like this:\r\n<code>%PM, %Europe\/Berlin. %487 %2008 u05 %R 121050249512<\/code>.\r\nMeanwhile I found out that the qTranslate functions can also only deal with a <a rel=\"external\"  href=\"http:\/\/www.php.net\/manual\/en\/function.date.php\"><code>date()<\/code>&nbsp;format<\/a>, although the author <a rel=\"external\"  href=\"http:\/\/www.qianqin.de\/qtranslate\/forum\/viewtopic.php?f=3&#038;t=3\">stated explicitly<\/a> that the functions would expect a <a rel=\"external\"  href=\"http:\/\/www.php.net\/manual\/en\/function.strftime.php\"><code>strftime()<\/code>&nbsp;format<\/a>. I don&#8217;t know why that is &#8211; maybe because I am using WordPress&nbsp;2.5, which is not fully supported by qTranslate so far. Anyway, it does explain the strange output, because when using <code>date()<\/code>, <code>A<\/code> stands for a capitalised &#8220;AM&#8221; or &#8220;PM&#8221;, <code>e<\/code> stands for the time zone etc.<\/p>\r\n\r\n<p>As for the comments dates anyhow, I was forced to use the date function provided by qTranslate, because I couldn&#8217;t find a native WordPress function, which simply returns the date instead of immediately displaying it. Since I had figured out now how the qTranslate functions really work, I solved this as follows:<\/p>\r\n\r\n<pre>\r\n_e(\r\n    '&lt;!--:de--&gt;'.htmlentities(\r\n        strftime('%A, %e. %B %Y um %R Uhr', \r\n            strtotime(\r\n                qtrans_formatCommentDateTime('Y\/m\/d H:i')\r\n            )\r\n        )\r\n    ).'&lt;!--:--&gt;&lt;!--:en--&gt;'.htmlentities(\r\n        strftime('%A, %e %B %Y at %I:%M %P', \r\n            strtotime(\r\n                qtrans_formatCommentDateTime('Y\/m\/d H:i')\r\n            )\r\n        )\r\n    ).'&lt;!--:--&gt;'\r\n)\r\n<\/pre>","protected":false},"excerpt":{"rendered":"Note: This post is quite old. Especially the suggestions under items 2 and 3 are no longer appropriate. More recent tips for date formatting with qTranslate are available here. After updating to WordPress 2.5, I have &#8211; like many others, obviously &#8211; experienced the problem that post dates and times were not being localised anymore. [&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,82,79,606],"_links":{"self":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/94"}],"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=94"}],"version-history":[{"count":5,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":5742,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/94\/revisions\/5742"}],"wp:attachment":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}