Joomla Date Formatting

Posted in: Computers

Formatting the date and time in joomla is very easy.

Inside your module or component you might see some code that looks like this:

JText::_('%a, %B %d %Y at %l:%M %p')

Or this..

JText::_('DATE_FORMAT_LC2')

In the first example, each of the % signs followed by a letter indicates what is to displayed.  The above example will display: Thur, October 15 2009 at 4:59 PM.  Here is a reference to what these date and time symbols do:

%a - Abbreviated Day of the Week
%A - Full Name Day of the Week
%H - Hour 00 to 24
%l - Hour 1 to 12
%h - Hour 1 to 12
%m - Numerical Month (01 to 12)
%B - Textual Name of Month
%d - Numerical Day
%Y - 4 Digit Year
%y - 2 Digit Year
%M - Minutes
%S - Seconds
%p - AM or PM

Something to take note of is that I couldn't get %l and %h to work on my localhost installation.  It does work on my remote installation, and I'm not exactly sure why.

In the second example, a universal time format is used. These formats are specified in the /joomla/language/en-GB/en-GB.ini/ file. If you want to create your own custom date format, open up the file and create something like this:

HARVEST_CUSTOM_DATE_FORMAT = %B %d, %Y

Now you can substitute the variable wherever you want that date format:

JText::_('HARVEST_CUSTOM_DATE_FORMAT')