Marco's Blog

All content personal opinions or work.
en eo

Publishing with Dates and Times in Joomla 3.x (and Time Zones, Too)

2016-01-25 5 min read Joomla marco

I love Joomla like I love my favorite sweater: it’s old, it’s ratty, but it’s comfortable and does its job marvelously. Unlike the sweater, it’s not easy to replace. Also unlike the sweater, I know how to teach it new things.

When publishing articles, Joomla insists on displaying the date in the format, “day Month year.” Nothing wrong with that, but I’d generally want the day of the week, too. And maybe the time in certain categories, like surfing.

It turns out it’s relatively easy to change the behavior, as it’s something the good people of joomla.org thought about (unlike the behavior of the Prev/Next button, that is hard-coded to follow category with no option to go through all articles chronologically).

Theory: the display of the date chunk is controlled in layout files stored under layouts/joomla/content/info_block. The info block we are interested in, in this case, is created_date.php. If you look at that file (it’s very short), you’ll notice that the relevant line is:

<?php echo JText::sprintf('COM_CONTENT_CREATED_DATE_ON', JHtml::_('date', $displayData['item']->created, JText::_('DATE_FORMAT_LC3'))); ?>

What on earth is this gobbledygood, you say? Well, let’s break it down. First, there is JText, a Joomla class that handles text in a general fashion. The basic idea is that you call JText methods, and JText returns strings appropriate for the conditions. It may return the text in the language chosen by the user. Or it may return something overridden from the default under certain conditions.

Here, the sprintf method is called. You don’t really need to know much about it, only that the first argument is looked up in the language strings and merged with the following arguments. There is only one argument following, and it’s a call to JHtml. That’s another utility class that allows you to format things appropriately for an HTML display. In this case, we tell Joomla we want a ‘date’ item displayed. The date item we want is the ->created date.

The last item is the format to use. It’s again a call to JText, so we can use Joomla’s internal configuration mechanism to make it return whatever string we like. What we need to do is change the return value for the DATE_FORMAT_LC3 variable.

Practice: Armed with the theory, we figure out different ways we can perform the change. The most dramatic way is to go to the file that contains the original definition, languages/lc-CC/lc-CC.ini (where lc stands for language code, like en for English, and cc for country code, like GB for Great Britain, which are the Joomla defaults). There, you will find a series of DATE_FORMAT definitions that are made up of strange letter sequences. The definition we want to change, LC3, says:

DATE_FORMAT_LC3=”d F Y”

What does that mean? The letters represent date parts. d is for day, F for full month, Y for year. That’s what we want to change.

Turns out we don’t need to change this file, though. That’s good, because this file gets overwritten with pretty much every Joomla update, and it would be a pain to have to remember to go back and modify this every time you update.

Instead, Joomla provides an easy way to override language constants. The details may vary from release to release, but generally you follow these steps:

  1. Log in to Administrator
  2. Either from the Extensions menu at the top or from the left side navigation, select “Language(s)”
  3. Select “Overrides” in the left nav
  4. Click the “New” button on top
  5. Enter DATE_FORMAT_LC3 in the Language Constant box
  6. Enter the date format you chose in the Text box
  7. Save and close
  8. (IMPORTANT) Log out and log back in

Ta-dah! Now, if you need to know what the different letter are so that you can pick the format you like, they are simply PHP formatting constants. You can look up their values here. You have actually pretty fine-grained control – or you can go totally lazy. If you simply enter the letter ‘r’, you get something formatted like this:

Thu, 21 Dec 2000 16:01:07 +0200

You should play around with different formats and see what you like for your users.

Time zones. As an added bonus, info on how to handle time zones. That’s been really annoying to me, since the entries with time suddenly were in the wrong time zone. In particular, by default Joomla lives in UTC, which is in essence UK time.

There are two places where you can change the time zone. One is the time zone of the server, the other the time zone of the user. If you change the server time zone, new users get assigned the new time zone, but old users still live in their old one.

To change the time zone of the server:

  1. Log in to Administrator
  2. Select System > Global configuration from the menu or Global from the left nav
  3. Select the “Server” tab on top
  4. Pick the correct Server Time Zone under Location Settings. If you don’t know what the name of the correct zone is, go to time.org
  5. Save and close

To change the time zone of the user as an administrator:

  1. Log in to Administrator
  2. Menu: Users > Manage or Users left side nav
  3. Click on the user you want to change
  4. Click on the Basic Settings tab
  5. Select the correct time zone
  6. Save & Close
  7. (IMPORTANT) The change takes effect when the user next logs in. So if you are testing, log out and log back in.