API16:JHtml/date
From Joomla! Documentation
Description
Returns formated date according to a given format and time zone.
Template:Description:JHtml/date
Syntax
static date($input= 'now', $format=null, $tz=true)
| Parameter Name | Default Value | Description |
|---|---|---|
| $input | 'now' | String in a format accepted by strtotime(), defaults to "now". |
| $format | null | format optional format for strftime |
| $tz | true | Time zone to be used for the date. Special cases: boolean true for user setting, boolean false for server setting. |
Returns
string A date translated by the given format and time zone.
Defined in
libraries/joomla/html/html.php
Importing
jimport( 'joomla.html.html' );
Source Body
public static function date($input = 'now', $format = null, $tz = true)
{
// Get some system objects.
$config = JFactory::getConfig();
$user = JFactory::getUser();
// UTC date converted to user time zone.
if ($tz === true)
{
// Get a date object based on UTC.
$date = JFactory::getDate($input, 'UTC');
// Set the correct time zone based on the user configuration.
$date->setOffset($user->getParam('timezone', $config->getValue('config.offset')));
}
// UTC date converted to server time zone.
elseif ($tz === false)
{
// Get a date object based on UTC.
$date = JFactory::getDate($input, 'UTC');
// Set the correct time zone based on the server configuration.
$date->setOffset($config->getValue('config.offset'));
}
// No date conversion.
elseif ($tz === null)
{
$date = JFactory::getDate($input);
}
// UTC date converted to given time zone.
else
{
// Get a date object based on UTC.
$date = JFactory::getDate($input, 'UTC');
// Set the correct time zone based on the server configuration.
$date->setOffset($tz);
}
// If no format is given use the default locale based format.
if (!$format) {
$format = JText::_('DATE_FORMAT_LC1');
}
return $date->toFormat($format);
}
[Edit See Also] Template:SeeAlso:JHtml/date
Examples
<CodeExamplesForm />
// Server Timezone: "New York" (-0500 GMT)
// User Timeszone: "Los Angeles" (-0800 GMT)
jimport( 'joomla.html.html' );
$sqlGmtTimestamp = "2012-03-01 20:00:00"
echo JHtml::date($sqlGmtTimestamp , 'D F n, Y g:i a'); // Fri March 1, 2012 12:00 pm
echo JHtml::date($sqlGmtTimestamp , 'D F n, Y g:i a', true); // Fri March 1, 2012 12:00 pm
echo JHtml::date($sqlGmtTimestamp , 'D F n, Y g:i a', false); // Fri March 1, 2012 3:00 pm