Cómo utilizar JDate

From Joomla! Documentation

Revision as of 15:16, 29 October 2015 by Abulafia (talk | contribs) (Created page with "Cómo utilizar JDate")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎русский • ‎中文(台灣)‎


JDate is a helper class, extended from PHP's DateTime class, which allows developers to handle date formatting more efficiently. The class allows developers to format dates for readable strings, MySQL interaction, UNIX timestamp calculation, and also provides helper methods for working in different timezones.

Using JDate

Creating a JDate Instance

All of the date helper methods require an instance of the JDate class. To begin, you must create one. A JDate object may be created in two ways. One is the typical native method of simply creating a new instance:

$date = new JDate(); // Creates a new JDate object equal to the current time.

You may also create an instance using the static method defined in JDate:

$date = JDate::getInstance(); // Alias of 'new JDate();'

There is no difference between these methods, as JDate::getInstance simply creates a new instance of JDate exactly like the first method shown.

Alternatively, you may also retrieve the current date (as a JDate object) from JApplication, by using:

$date = JFactory::getDate();


The JDate constructor (and getInstance static method) accepts two optional parameters: A date string to format, and a timezone. Not passing a date string will create a JDate object with the current date and time, while not passing a timezone will allow the JDate object to use the default timezone set.

The first argument, if used, must be a string that can be parsed using php's native DateTime constructor. E.g:

$currentTime = new JDate('now'); // Current date and time
$tomorrowTime = new JDate('now +1 day'); // Current date and time, + 1 day.
$date = new JDate('2012-12-1 15:20:00'); // 3:20 PM, December 1st, 2012

Temporary example

JFactory::getDate() gets a JDate object and we then do the JDate toFormat function

JFactory::getDate()->toFormat('%a %d %b %Y - %H:%M')

In Joomla! CMS 3.3 API the function "toFormat" was changed to "format", so the example above should be:

JFactory::getDate()->format('%a %d %b %Y - %H:%M')