Swaped values when using Zend_Date::get() with Zend_Date::YEAR or Zend_Date::YEAR_SHORT #50

zfbot opened this Issue Apr 5, 2013 · 4 comments


None yet

3 participants


Jira Information

Original Issue: ZF-12510
Issue Type: Bug
Reporter: Mário Pereira
Created: 01/18/13
Components: Zend_Date


When trying to retrieve the year part of a Zend_Date, the constants Zend_Date::YEAR and Zend_Date::YEAR_SHORT return each others value.

Code sample:

$today = new Zend_Date(); // assume it is 2013-01-18 11:00:00
$year = $today->get(Zend_Date::YEAR); // $year should be "2013", it is "13"
$shortYear = $today->get(Zend_Date::YEAR_SHORT); // $yearShort should be "13", it is "2013"


(Originally posted by: froschdesign on 01/18/13)

Hi Mário,
I can not reproduce your problem. All unit tests are okay.

Here is a test for your example:

public function testDateShouldMatchLongAndShortYear()
    $date = new Zend_Date('31.12.2007');

    $this->assertEquals('2007', $date->get(Zend_Date::YEAR));
    $this->assertEquals('07', $date->get(Zend_Date::YEAR_SHORT));

Result: OK


(Originally posted by: Mário Pereira on 01/18/13)

You are right, there was a piece of information missing that I overlooked.

This behaviour only occurs when you set the 'format' option of Zend_Date to 'php'. I was setting it in the bootstrap and overlooked it in the test code I supplied.

I created an empty project just to test this and found the following: if I use Zend_Date::setOptions(array('format_type' => 'php')), then I get the results I said above. If I do not set that Zend_Date option, then the calls to get work as expected.

// no call to Zend_Date::setOptions();
$today = new Zend_Date();
$year = $today->get(Zend_Date::YEAR); // returns full 4 digit year
$shortYear = $today->get(Zend_Date::YEAR_SHORT); // returns 2 digit year


Zend_Date::setOptions(array('format_type' => 'php'));
$today = new Zend_Date();
$year = $today->get(Zend_Date::YEAR); // returns 2 digit year
$shortYear = $today->get(Zend_Date::YEAR_SHORT); // returns full 4 digit year

Is this an expected behaviour? I've been browsing the documentation and it does not say anything about Zend_Date::get() behaviour changing with or without this option.


This issue was ported from the ZF2 Jira Issue Tracker at

Known GitHub users mentioned in the original message or comment:


Looking at the zend framework source code this is the expected behaviour.

In your second example when the get method is called on an instance of Zend_Date with an option of

format_type => php

the first argument that is parsed to the class method get is expected to be a date in the php date format
http://php.net/manual/en/function.date.php which is converted to iso format. See code snippet below.
Looking at the tests for the Date class also confirms this.

public function get($part = null, $locale = null)
} else if (self::$_options['format_type'] == 'php') {
    $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);

So in the first example
Zend_Date::YEAR which equals y is left as y in the get method and
Zend_Date::YEAR_SHORT which equals yy is left as yy in the get method

but in the second an example when $_options['format_type'] == 'php'
Zend_Date::YEAR which equals y is converted to yy in the get method and
Zend_Date::YEAR_SHORT which equals yy is converted to yyyy

Therefore in my opinion this is not a bug but the valid output. It is the expected behaviour.

The documentation could perhaps be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment