summaryrefslogtreecommitdiff
path: root/includes/common.inc
diff options
context:
space:
mode:
authorKjartan Mannes <kjartan@2.no-reply.drupal.org>2004-02-08 21:42:59 +0000
committerKjartan Mannes <kjartan@2.no-reply.drupal.org>2004-02-08 21:42:59 +0000
commit97c2aa2bda7c25e89770b25f5810cc544272c08c (patch)
treef0636119d58415da7194712ee42f022541d17685 /includes/common.inc
parent325f9891b28ef3d1bb8f385057ad487ce9e9c79e (diff)
downloadbrdo-97c2aa2bda7c25e89770b25f5810cc544272c08c.tar.gz
brdo-97c2aa2bda7c25e89770b25f5810cc544272c08c.tar.bz2
- Modified format_date() to handle timezones properly.
Diffstat (limited to 'includes/common.inc')
-rw-r--r--includes/common.inc48
1 files changed, 32 insertions, 16 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 6e4c5eb22..3f20e3f15 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -844,39 +844,55 @@ function format_interval($timestamp, $granularity = 2) {
* should contain the format string
* @param $format Format string (as required by the PHP date() function).
* Only required if 'custom' date format is requested.
+ * @param $timezone Timezone offset in seconds in case the user timezone
+ * should not be used.
* @return Translated date string in the requested format
*/
-function format_date($timestamp, $type = "medium", $format = "") {
- global $user;
+function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL) {
+ if ($timezone === NULL) {
+ global $user;
+ $timezone = $user->uid ? $user->timezone : variable_get('date_default_timezone', 0);
+ }
- $timestamp += ($user->timezone) ? $user->timezone - date("Z") : 0;
+ $timestamp += $timezone;
switch ($type) {
- case "small":
- $format = variable_get("date_format_short", "m/d/Y - H:i");
+ case 'small':
+ $format = variable_get('date_format_short', 'm/d/Y - H:i');
break;
- case "large":
- $format = variable_get("date_format_long", "l, F j, Y - H:i");
+ case 'large':
+ $format = variable_get('date_format_long', 'l, F j, Y - H:i');
break;
- case "custom":
+ case 'custom':
// No change to format
break;
- case "medium":
+ case 'medium':
default:
- $format = variable_get("date_format_medium", "D, m/d/Y - H:i");
+ $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
}
- for ($i = strlen($format); $i >= 0; $c = $format[--$i]) {
- if (strstr("AaDFlM", $c)) {
- $date = t(date($c, $timestamp)) . $date;
+ $max = strlen($format);
+ for ($i = 0; $i <= $max; $c = $format{$i++}) {
+ if (strpos('AaDFlM', $c)) {
+ $date .= gmdate($c, $timestamp);
+ }
+ else if (strpos('BdgGhHiIjLmnsStTUwWYyz', $c)) {
+ $date .= gmdate($c, $timestamp);
+ }
+ else if ($c == 'r') {
+ $date .= format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone);
}
- else if (strstr("BdgGhHiIjLmnOrsStTUwWYyZz", $c)) {
- $date = date($c, $timestamp) . $date;
+ else if ($c == 'O') {
+ $date .= sprintf('%s%02d%02d', ($timezone < 0 ? '-' : '+'), abs($timezone / 3600), abs($timezone % 3600) / 60);
+ }
+ else if ($c == 'Z') {
+ $date .= $timezone;
}
else {
- $date = $c.$date;
+ $date .= $c;
}
}
+
return $date;
}