/** * Convert unix time to a human readable time in the user's timezone or in a * given timezone. * * For supported timezones visit - http://php.net/manual/timezones.php * For accepted formats visit - http://php.net/manual/function.date.php * * @example echo user_time(); * @example echo user_time($timestamp, 'EET', 'l jS \of F Y h:i:s A'); * * @param int $timestamp A UNIX timestamp. If none is given, current time * will be used. * @param string $timezone The destination timezone for the conversion. If * none is given, the current user's configured timezone will be used. * @param string $format The format string to apply to the converted * timestamp. * * @return string A string containing the timestamp in the requested format. */ function user_time($timestamp = null, $timezone = null, $format = 'r') { if (!$timezone) { $CI =& get_instance(); $CI->load->library('securinator/Auth'); if ($CI->auth->is_logged_in()) { $timezone = standard_timezone($CI->auth->user()->timezone); } } if (empty($timestamp)) { $dtime = new DateTime(null, new DateTimeZone($timezone)); return $dtime->format($format); } $dtime = new DateTime(); if (is_int($timestamp)) { $dtime->setTimestamp($timestamp); } elseif ($timestamp != 'now') { $dtime->modify($timestamp); } return $dtime->setTimezone(new DateTimeZone($timezone))->format($format); }
/** * Converts unix time to a human readable time in user timezone * or in a given timezone. * * For supported timezones visit - http://php.net/manual/timezones.php * For accepted formats visit - http://php.net/manual/function.date.php * * @example echo user_time(); * @example echo user_time($timestamp, 'EET', 'l jS \of F Y h:i:s A'); * * @param int $timestamp A UNIX timestamp. If non is given current time will be used. * @param string $timezone The timezone we want to convert to. If none is given a current logged user timezone will be used. * @param string $format The format of the outputted date/time string * * @return string A string formatted according to the given format using the given timestamp and given timezone or the current time if no timestamp is given. */ function user_time($timestamp = NULL, $timezone = NULL, $format = 'r') { if (!$timezone) { $CI =& get_instance(); $CI->load->library('users/auth'); if ($CI->auth->is_logged_in()) { $timezone = standard_timezone($CI->auth->user()->timezone); } } $timestamp = $timestamp ? $timestamp : time(); $dtzone = new DateTimeZone($timezone); $dtime = new DateTime(); return $dtime->setTimestamp($timestamp)->setTimeZone($dtzone)->format($format); }