Details
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
}
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.