summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralecpl <alec@alec.pl>2012-04-07 16:28:52 +0000
committeralecpl <alec@alec.pl>2012-04-07 16:28:52 +0000
commit8e8ae79cd0fbe30e8ca4e917d94166efb4336f2e (patch)
tree5327826eed18b143bd6f5800e572ee05d718e4f9
parent19fccd8b220e9ed86301b4efc82551bc7769aa2d (diff)
- Fix format_date() when $convert=false (#1488147)
-rw-r--r--program/include/main.inc57
1 files changed, 31 insertions, 26 deletions
diff --git a/program/include/main.inc b/program/include/main.inc
index 51a65b505..3489928cd 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -735,38 +735,36 @@ function format_date($date, $format=NULL, $convert=true)
{
global $RCMAIL, $CONFIG;
- if (is_a($date, 'DateTime')) {
- $ts = $date->format('U');
- $tzs = $date->getTimezone();
+ if (is_object($date) && is_a($date, 'DateTime')) {
+ $timestamp = $date->format('U');
}
else {
- $tzs = 'GMT';
-
if (!empty($date))
- $ts = rcube_strtotime($date);
+ $timestamp = rcube_strtotime($date);
- if (empty($ts))
+ if (empty($timestamp))
return '';
try {
- $date = new DateTime("@".$ts);
+ $date = new DateTime("@".$timestamp);
}
catch (Exception $e) {
return '';
}
}
- try {
- // convert to the right timezone
- $stz = date_default_timezone_get();
- $tz = new DateTimeZone($convert ? $RCMAIL->config->get('timezone') : $tzs);
- $date->setTimezone($tz);
- date_default_timezone_set($tz->getName());
+ if ($convert) {
+ try {
+ // convert to the right timezone
+ $stz = date_default_timezone_get();
+ $tz = new DateTimeZone($RCMAIL->config->get('timezone'));
+ $date->setTimezone($tz);
+ date_default_timezone_set($tz->getName());
- $timestamp = $date->format('U');
- }
- catch (Exception $e) {
- $timestamp = $ts;
+ $timestamp = $date->format('U');
+ }
+ catch (Exception $e) {
+ }
}
// define date format depending on current time
@@ -789,14 +787,18 @@ function format_date($date, $format=NULL, $convert=true)
// strftime() format
if (preg_match('/%[a-z]+/i', $format)) {
$format = strftime($format, $timestamp);
- date_default_timezone_set($stz);
+
+ if ($convert && $stz) {
+ date_default_timezone_set($stz);
+ }
+
return $today ? (rcube_label('today') . ' ' . $format) : $format;
}
// parse format string manually in order to provide localized weekday and month names
// an alternative would be to convert the date() format string to fit with strftime()
$out = '';
- for($i=0; $i<strlen($format); $i++) {
+ for ($i=0; $i<strlen($format); $i++) {
if ($format[$i]=='\\') // skip escape chars
continue;
@@ -805,20 +807,20 @@ function format_date($date, $format=NULL, $convert=true)
$out .= $format[$i];
// weekday (short)
else if ($format[$i]=='D')
- $out .= rcube_label(strtolower($date->format('D')));
+ $out .= rcube_label(strtolower(date('D', $timestamp)));
// weekday long
else if ($format[$i]=='l')
- $out .= rcube_label(strtolower($date->format('l')));
+ $out .= rcube_label(strtolower(date('l', $timestamp)));
// month name (short)
else if ($format[$i]=='M')
- $out .= rcube_label(strtolower($date->format('M')));
+ $out .= rcube_label(strtolower(date('M', $timestamp)));
// month name (long)
else if ($format[$i]=='F')
- $out .= rcube_label('long'.strtolower($date->format('M')));
+ $out .= rcube_label('long'.strtolower(date('M', $timestamp)));
else if ($format[$i]=='x')
$out .= strftime('%x %X', $timestamp);
else
- $out .= $date->format($format[$i]);
+ $out .= date($format[$i], $timestamp);
}
if ($today) {
@@ -832,7 +834,10 @@ function format_date($date, $format=NULL, $convert=true)
}
}
- date_default_timezone_set($stz);
+ if ($convert && $stz) {
+ date_default_timezone_set($stz);
+ }
+
return $out;
}