2

坦率地说,我很困惑。谁能告诉我为什么我会收到此代码的失败消息?

$date = Zend_Date::now();
$date = $date->getIso();

if(Zend_Date::isDate($date, Zend_Date::ISO_8601)) {
    print('success');
} else {
    print('failure');
}

exit;

如果我只传入一个 Zend_Date 对象,它也会失败。

更新:

初始 $date 对象的 var_dump 如下所示:

object(Zend_Date)#107 (8) { ["_locale:private"]=> string(5) "en_US" ["_fractional:private"]=> int(0) ["_precision:private"]=> int( 3) ["_unixTimestamp:private"]=> int(1257508100) ["_timezone:private"]=> string(14) "美国/丹佛" ["_offset:private"]=> int(25200) ["_syncronised:私有的"]=> int(0) ["_dst:protected"]=> bool(true) }

调用 $date->getIso() 后 $date 字符串的 var_dump 如下所示:

字符串(25)“2009-11-06T04:48:20-07:00”

我在 PHP 5.2.8 上使用 ZF 1.9.5。如果这有所作为,我也在使用 XAMPP for Windows。

4

2 回答 2

3

我在 Ubuntu 上运行 ZF 1.9.4 和 PHP 5.2.10,并且能够重现您遇到的完全相同的问题。作为好奇的类型,我做了一点挖掘。在 isDate 的代码中,首先调用伴随类 Zend_Locale_Format 中的 getDate。这是一个 try-catch 循环,所以在 catch 部分,我让它将异常转储到 stdout。这是异常转储向我展示的内容:

带有消息“无法解析日期”的异常“Zend_Locale_Exception”
'2009-11-06T04:26:46-08:00' 在 /usr/share/php/libzend-framework-php/Zend/Locale/Format.php:995 中使用 'dd mm yy' (dy)'
堆栈跟踪:
#0 /usr/share/php/libzend-framework-php/Zend/Locale/Format.php(1116): Zend_Locale_Format::_parseDate('2009-11-06T04:2...', 数组)
#1 /usr/share/php/libzend-framework-php/Zend/Date.php(4583): Zend_Locale_Format::getDate('2009-11-06T04:2...', 数组)
#2 {censored}/testbed/test.php(26): Zend_Date::isDate('2009-11-06T04:2...', 'c')
#3 {主要}

对这个异常做一个 var_dump 更能说明那些不透明的数组。它们中的每一个都包含以下内容:

数组(4){                                                                       
          [“地区”]=>                                                                   
          字符串(5)“en_US”                                                              
          ["date_format"]=>                                                              
          string(8) "dd mm yy"                                                           
          ["format_type"]=>                                                              
          string(3) "iso"                                                                
          ["fix_date"]=>                                                                 
          bool(false)                                                                    
        }           

So, date_format doesn't look right at all. It should be "YYYYMMDD'T'hh:mm:ssP," or something like that, in PHP date formatting lingo (I quoted the T, since it's the literal 'T' and not a timezone abbreviation). Granted, PHP just abbreviates it as 'c'.

Strange. So where in the world is it getting this date format? From _getLocalizedToken:

 protected static function _getLocalizedToken($token, $locale)
    {
        switch($token) {
            case self::ISO_8601 :
                return "dd mm yy";
                break;
...

That format looks completely wrong, given the output that ISO_8601 produces.

I would probably check with the people on the appropriate Zend list, but at first glance, this looks like something worthy of a bug report. Maybe they just don't support checks this particular type of date string yet?

于 2009-11-06T12:45:45.057 回答
0

Sorry I read this so long after you posted this. The code that goes wrong for you works for me, try to use the Zend/Date class standalone in the php source like this:


set_include_path('./lib' . PATH_SEPARATOR . get_include_path());
include_once 'Zend/Date.php';
...

And you probably also need to set php.ini like this:

include_path = ".:/usr/share/php:/usr/share/php/smarty/libs:/opt/zend/library"

Or where your zend libs are.

Then it shows 'success'. Hope that helps.

Edit: You probably also want to DISABLE plain Zend use, I only wanted the Date class as it kinda rocks afaik.

Edit: I use Zend extension 220060519 which seems to be Zend Engine v2.2.0./ PHP 5.2.6-1+lenny8 When I was googling this I came accross a few links that explained there is(was?) another Date class section in the zend framework which weren't the same. But I could be wrong, this link helped me finding the cause though but I have short memory concerning my thought process at the time :)

于 2010-04-14T20:38:25.433 回答