public static string
date
(mixed $input = 'now', mixed $format = null, mixed $tz = true, mixed $gregorian = false)
/**
* Returns formatted date according to a given format and time zone.
*
* @param string $input String in a format accepted by date(), defaults to "now".
* @param string $format The date format specification string (see {@link PHP_MANUAL#date}).
* @param mixed $tz Time zone to be used for the date. Special cases: boolean true for user
* setting, boolean false for server setting.
* @param boolean $gregorian True to use Gregorian calendar.
*
* @return string A date translated by the given format and time zone.
*
* @see strftime
* @since 1.5
*/
public static function date($input = 'now', $format = null, $tz = true, $gregorian = false)
{
$app = Factory::getApplication();
// UTC date converted to user time zone.
if ($tz === true) {
// Get a date object based on UTC.
$date = Factory::getDate($input, 'UTC');
// Set the correct time zone based on the user configuration.
$date->setTimezone($app->getIdentity()->getTimezone());
} elseif ($tz === false) {
// Get a date object based on UTC.
$date = Factory::getDate($input, 'UTC');
// Set the correct time zone based on the server configuration.
$date->setTimezone(new \DateTimeZone($app->get('offset')));
} elseif ($tz === null) {
$date = Factory::getDate($input);
} else {
// Get a date object based on UTC.
$date = Factory::getDate($input, 'UTC');
// Set the correct time zone based on the server configuration.
$date->setTimezone(new \DateTimeZone($tz));
}
// If no format is given use the default locale based format.
if (!$format) {
$format = Text::_('DATE_FORMAT_LC1');
} elseif (Factory::getLanguage()->hasKey($format)) {
$format = Text::_($format);
}
if ($gregorian) {
return $date->format($format, true);
}
return $date->calendar($format, true);
}