Zend Framework

Formatted month and day values are sometimes inverted

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Not an Issue
  • Affects Version/s: 1.0.2
  • Fix Version/s: 1.0.3
  • Component/s: Zend_Date
  • Labels:
    None

Description

This appears to happen when the month of the given date is greater than 9. See the code below for an example exposing the bug. Note that the first call to dateTest produces the expected output, while the month and day are inverted in the fourth line of the output for the second call when strtotime() is not called on the parameter before passing it to the Zend_Date constructor, whereas the output is correct when strtotime() is called on the parameter before passing it to the Zend_Date constructor.

DateTest.php
dateTest('2007-09-07 23:08:00');
/*
Output:
$datetime = 2007-09-07 23:08:00
strtotime($date) = 1189224480
date('Y-m-d H:i:s', strtotime($datetime)) = 2007-09-07 23:08:00
(new Zend_Date($datetime))->toString('YYYY-MM-dd HH:mm:ss') = 2007-07-09 23:08:00
(new Zend_Date(strtotime($datetime)))->toString('YYYY-MM-dd HH:mm:ss') = 2007-09-07 23:08:00
*/

dateTest('2007-10-09 23:08:00');
/*
Output:
$datetime = 2007-10-09 23:08:00
strtotime($date) = 1191989280
date('Y-m-d H:i:s', strtotime($datetime)) = 2007-10-09 23:08:00
(new Zend_Date($datetime))->toString('YYYY-MM-dd HH:mm:ss') = 2007-09-10 23:08:00
(new Zend_Date(strtotime($datetime)))->toString('YYYY-MM-dd HH:mm:ss') = 2007-10-09 23:08:00
*/

function dateTest($datetime)
{
    echo '$datetime = ' . $datetime . "\n";
    echo 'strtotime($date) = ' . strtotime($datetime) . "\n";
    echo 'date(\'Y-m-d H:i:s\', strtotime($datetime)) = ' . 
        date('Y-m-d H:i:s', strtotime($datetime)) . "\n";
    $date = new Zend_Date($datetime);
    echo '(new Zend_Date($datetime))->toString(\'YYYY-MM-dd HH:mm:ss\') = ' . 
        $date->toString('YYYY-MM-dd HH:mm:ss') . "\n";
    $date = new Zend_Date(strtotime($datetime));
    echo '(new Zend_Date(strtotime($datetime)))->toString(\'YYYY-MM-dd HH:mm:ss\') = ' . 
        $date->toString('YYYY-MM-dd HH:mm:ss
}

Activity

Hide
Thomas Weidner added a comment -

What is your default locale ?
I expect that's your problem...

Reason:
In your second example you gave an integer to Zend_Date.
Integers are treated as unix timestamps. This is why this example works in your eyes.

In your first example you gave an string to Zend_Date.
Strings are treated as localized inputs. If you do not provide an locale Zend_Date uses your default locale from your browser or from your server.

The problem occurs when you give an input which differs from your default locale.
Example:
When you give '10.04.2007 10:00:00' and your default locale is 'YYYY-MM-dd HH:mm:ss' your input will not be parsed correct. There is some automatically correction... 2007 can never be a month for example, and so on.
But days and months smaller then 13 can not automatically be corrected.

But every "normal" user will give an input in the format he is familiar with.
Or you define a input format which the user hsa to do... then use the optional parameters of Zend_Date.

Show
Thomas Weidner added a comment - What is your default locale ? I expect that's your problem... Reason: In your second example you gave an integer to Zend_Date. Integers are treated as unix timestamps. This is why this example works in your eyes. In your first example you gave an string to Zend_Date. Strings are treated as localized inputs. If you do not provide an locale Zend_Date uses your default locale from your browser or from your server. The problem occurs when you give an input which differs from your default locale. Example: When you give '10.04.2007 10:00:00' and your default locale is 'YYYY-MM-dd HH:mm:ss' your input will not be parsed correct. There is some automatically correction... 2007 can never be a month for example, and so on. But days and months smaller then 13 can not automatically be corrected. But every "normal" user will give an input in the format he is familiar with. Or you define a input format which the user hsa to do... then use the optional parameters of Zend_Date.
Hide
Thomas Weidner added a comment -

Btw:
strtotime uses the english notation...

So if you call Zend_Date with the locale 'en' it works like strtotime.

Show
Thomas Weidner added a comment - Btw: strtotime uses the english notation... So if you call Zend_Date with the locale 'en' it works like strtotime.
Hide
Matthew Turland added a comment -

I tried changing this line:

$date = new Zend_Date($datetime);

to this:

$date = new Zend_Date($datetime, Zend_Date::DATES, 'en_US')

as per examples from the documentation and your suggestion, which I'm assuming is what you were inferring.

http://framework.zend.com/manual/en/zend.date.overview.html#zend.date.options

The output is identical. This is under PHP 5.2.4, if that's relevant.

Show
Matthew Turland added a comment - I tried changing this line: $date = new Zend_Date($datetime); to this: $date = new Zend_Date($datetime, Zend_Date::DATES, 'en_US') as per examples from the documentation and your suggestion, which I'm assuming is what you were inferring. http://framework.zend.com/manual/en/zend.date.overview.html#zend.date.options The output is identical. This is under PHP 5.2.4, if that's relevant.
Hide
Thomas Weidner added a comment -

No... you are assuming wrong.

The standard format for en_US is "MMM d, yyyy"...
You are giving "YYYY-MM-dd" as input.

So eighter you use the right locale-input
OR
if you know that your input is always formatted as ISO you should always use the format Zend_Date::ISO_8601 for your input

Btw:
Running your code I am not receiving the output you gave... but this is because my default locale differs from yours.

Show
Thomas Weidner added a comment - No... you are assuming wrong. The standard format for en_US is "MMM d, yyyy"... You are giving "YYYY-MM-dd" as input. So eighter you use the right locale-input OR if you know that your input is always formatted as ISO you should always use the format Zend_Date::ISO_8601 for your input Btw: Running your code I am not receiving the output you gave... but this is because my default locale differs from yours.
Hide
Matthew Turland added a comment -

OK, changing the format to Zend_Date::ISO_8601 causes the output to be correct. You can go ahead and close this issue.

Show
Matthew Turland added a comment - OK, changing the format to Zend_Date::ISO_8601 causes the output to be correct. You can go ahead and close this issue.
Hide
Thomas Weidner added a comment -

Closing this issue as it's no "problem" of Zend_Date.

Tip: If you have a database value as input you should always use Zend_Date::ISO_8601 as stated in the documentation.

Show
Thomas Weidner added a comment - Closing this issue as it's no "problem" of Zend_Date. Tip: If you have a database value as input you should always use Zend_Date::ISO_8601 as stated in the documentation.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: