/** * 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 Application $app The application from which we'll retrieve settings, null to use the default app * * @return string A date translated by the given format and time zone. * * @see strftime */ public static function date($input = 'now', $format = null, $tz = true, Application $app = null) { if (!is_object($app)) { $app = Application::getInstance(); } // Get some system objects. $config = $app->getContainer()->appConfig; $userManager = $app->getContainer()->userManager; $user = $userManager->getUser(); // UTC date converted to user time zone. if ($tz === true) { // Get a date object based on UTC. $date = new Date($input, 'UTC'); // Set the correct time zone based on the user configuration. $date->setTimeZone(new \DateTimeZone($user->getParameters()->get('timezone', $config->get('timezone')))); } elseif ($tz === false) { // Get a date object based on UTC. $date = new Date($input, 'UTC'); // Set the correct time zone based on the server configuration. $date->setTimeZone(new \DateTimeZone($config->get('timezone', 'UTC'))); } elseif ($tz === null) { $date = new Date($input); } else { // Get a date object based on UTC. $date = new Date($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 (Text::hasKey($format)) { $format = Text::_($format); } return $date->format($format, true); }