Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[ZF-11992] Fixed issue with first day of year

- Internally, Zend_Date uses the ISO 8601 format "o" for the year. However, this
  is problematic; if the day requested falls in the week number of the last week
  of the previous year, the previous year will be reported. Added a preg_replace
  to change "o" to "Y" when not preceded by an escape.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24880 44c647ce-9c0f-0410-b52a-842ac1e357ba
  • Loading branch information...
commit cdfb6a52aa4fbfa01c39a7e4b02db9a86ffe93b1 1 parent 6edae56
matthew authored
View
8 library/Zend/Date.php
@@ -2644,10 +2644,8 @@ private function _calculate($calc, $date, $part, $locale)
$parsed['day'] = 0;
}
- if (isset($parsed['year'])) {
- $parsed['year'] -= 1970;
- } else {
- $parsed['year'] = 0;
+ if (!isset($parsed['year'])) {
+ $parsed['year'] = 1970;
}
}
@@ -2657,7 +2655,7 @@ private function _calculate($calc, $date, $part, $locale)
isset($parsed['second']) ? $parsed['second'] : 0,
isset($parsed['month']) ? (1 + $parsed['month']) : 1,
isset($parsed['day']) ? (1 + $parsed['day']) : 1,
- isset($parsed['year']) ? (1970 + $parsed['year']) : 1970,
+ $parsed['year'],
false), $this->getUnixTimestamp(), false);
} catch (Zend_Locale_Exception $e) {
if (!is_numeric($date)) {
View
7 library/Zend/Date/DateObject.php
@@ -312,6 +312,13 @@ protected function date($format, $timestamp = null, $gmt = false)
}
if (abs($timestamp) <= 0x7FFFFFFF) {
+ // See ZF-11992
+ // "o" will sometimes resolve to the previous year (see
+ // http://php.net/date ; it's part of the ISO 8601
+ // standard). However, this is not desired, so replacing
+ // all occurrences of "o" not preceded by a backslash
+ // with "Y"
+ $format = preg_replace('/(?<!\\\\)o\b/', 'Y', $format);
$result = ($gmt) ? @gmdate($format, $timestamp) : @date($format, $timestamp);
date_default_timezone_set($oldzone);
return $result;
View
9 tests/Zend/DateTest.php
@@ -5689,6 +5689,15 @@ public function testGetTimezoneFromStringForTimezoneOffsetsGreaterThan12()
$this->assertEquals('Etc/GMT-14', $date->getTimezoneFromString('18:00:00+1400'));
}
+ /**
+ * @group ZF-11992
+ */
+ public function testDateShouldMatchOnFirstDayOfYear()
+ {
+ $date = new Zend_Date('01.01.2012');
+ $out = $date->toString('Y-MM-dd');
+ $this->assertEquals('2012-01-01', $out);
+ }
}
class Zend_Date_TestHelper extends Zend_Date
Please sign in to comment.
Something went wrong with that request. Please try again.