How To Use Dates in Joomla

Joomla has a helper class JDate, which extends from PHP's DateTime class. It allows developers to handle date formatting more efficiently. JDate is a class that stores a date and provides logic to manipulate and render that date in a variety of formats.

Create a JDate Instance

You can retrieve the current date as a JDate object from JApplication, by using:

$date = JFactory::getDate();

You can use to set any date also:

$date = JFactory::getDate($any_date);
$date = JFactory::getDate('2017-12-05 15:20:00');

Change Date Format

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

PHP Dates and Time Formats (Click here for full article)

  • Month - F, M, m, n
  • Day - I, D, d, j, w
  • Year - y, Y
  • Hour - g. G. h, H
  • Minutes - i
  • Seconds - s
  • After / Before Noon - a, A

Output Date

$date = JFactory::getDate();
echo $date;
echo $date->format('%a %d %b %Y - %H:%M');
echo $date->dayofweek;

Methods in JDate

__construct(string $date = 'now', mixed $tz = null)

__get(string $name) : mixed

__toString() : string

add(\DateInterval $interval) : \Joomla\CMS\Date\Date|boolean

calendar(string $format, boolean $local = false, boolean $translate = true) : string

dayToString(integer $day, boolean $abbr = false) : string

format(string $format, boolean $local = false, boolean $translate = true) : string

getInstance(string $date = 'now', mixed $tz = null) : \Joomla\CMS\Date\Date

getOffsetFromGmt(boolean $hours = false) : float

modify(string $modify) : \Joomla\CMS\Date\Date|boolean

monthToString(integer $month, boolean $abbr = false) : string

setTimezone(\DateTimeZone $tz) : \Joomla\CMS\Date\Date

sub(\DateInterval $interval) : \Joomla\CMS\Date\Date|boolean

toISO8601(boolean $local = false) : string

toRFC822(boolean $local = false) : string

toSql(boolean $local = false, \JDatabaseDriver $db = null) : string

toUnix() : integer

Properties in JDate

format

daysinmonth

dayofweek

dayofyear

isleapyear

day

hour

minute

second

microsecond

month

ordinal

week

year

Subscribe for Free Updates

Please enable the javascript to submit this form