How to use a textual representation of a date in TYPOlight

By default, TYPOlight only supports numeric date formats like 2009-02-25 or 02/25/09. The problem with textual representations of days or months in PHP is that they are only available in the language of the server operating system. You can switch the server locale in PHP (see http://www.php.net/setlocale), however, it will affect the whole system (!) and not just the requested page, so that is surely not a useful solution for multilingual pages.

Native support for textual dates in version 2.7

From version 2.7, TYPOlight supports textual dates in the front end and in all custom modules and templates. All you have to do is to call $this->parseDate($format) instead of date($format). You do not need the workaround described below.

Using the TYPOlight language files

The TYPOlight language files include labels for days and months, which are e.g. used in the news archive menu. They can be used there, because the date format in this module never changes. In a news list, however, the date format is user-definable, so we have to add the translation manually if we want a textual representation of the date.

Module configuration

First of all, we have to set up a news list module and define the date format. It is important to set a numeric representation here, because we will be using it as an array key later on. In this example, I am going to make TYPOlight output the month name and the year (e.g. February 2009), so I use n to get a numeric representation of the month without leading zeros. You can use N to do the same for the day of the week.

Textual representation of a date - step 1

Template changes

Now that the news list module generates the correct date format, all we have to do is to modify the template. Open the template editor and copy the news template you are using (in this example, I am using news_latest.tpl).

Textual representation of a date - step 2

Once you have created a local copy, click the navigation icon to edit the template.

Textual representation of a date - step 3

Adding the PHP magic

In the template, the date is stored as $this->date. To override the month, we have to split the date string first.

1 <?php
2 list($month, $year) = explode(' ', $this->date);
3 ?>

Now $month is a numeric representation of the month and $year a numeric representation of the year. As I said at the beginning of the tutorial, we will now use $month as an array key to get the textual representation of the month.

1 <?php
2 $this->date = $GLOBALS['TL_LANG']['MONTHS'][($month - 1)] . ' ' . $year;
3 ?>

I am using ($month - 1) because months start with 1 but arrays start with 0.

Wrapping it all up

As you can see, it only takes two lines of PHP code to get a textual representation of a date.

1 <?php
2 list($month, $year) = explode(' ', $this->date);
3 $this->date = $GLOBALS['TL_LANG']['MONTHS'][($month - 1)] . ' ' . $year;
4 ?>

From version 2.7, all you have to do is to write $this->parseDate('n Y', $this->timestamp).

Insert the snippet at the top of your news template.

Textual representation of a date - step 4

Now TYPOlight should translate the month name correctly.

--- Tutorial created by Leo

textual_dates_1.png - Textual representation of a date - step 1 (26.9 KB) leo, 02/25/2009 01:09 pm

textual_dates_2.png - Textual representation of a date - step 2 (31.3 KB) leo, 02/25/2009 01:09 pm

textual_dates_3.png - Textual representation of a date - step 3 (22.4 KB) leo, 02/25/2009 01:10 pm

textual_dates_4.png - Textual representation of a date - step 4 (112.9 KB) leo, 02/25/2009 01:10 pm

Also available in: HTML TXT