PHP只在生产服务器上使用DateTime时出现奇怪的错误行为

时间:2012-08-18 00:46:02

标签: php datetime

简单地说:

    $end   = new \DateTime();
    $start = clone $end;

    // Remove 30 days from today and set midnight
    $start->sub(new \DateInterval('P30D'));
    $start->setTime(0, 0);

    var_dump(new \DateInterval('P30D'));
    var_dump($start, $end); die();

输出(格式良好):

object(DateInterval)#1516 (8)
{ ["y"]=> int(0)
  ["m"]=> int(0)
  ["d"]=> int(30)
  ["h"]=> int(0)
  ["i"]=> int(0)
  ["s"]=> int(0)
  ["invert"]=> int(0)
  ["days"]=> int(0) }

object(DateTime)#1523 (3)
{ ["date"]=> string(19) "2012-06-19 00:00:00"
  ["timezone_type"]=> int(3)
  ["timezone"]=> string(11) "Europe/Rome"
}

object(DateTime)#4682 (3)
{ ["date"]=> string(19) "2012-08-18 02:42:23"
  ["timezone_type"]=> int(3)
  ["timezone"]=> string(11) "Europe/Rome"
}

1个月的错误......任何线索?

编辑:仅在使用时才能正确行为:

$start->modify('-30 days');

评论DateInterval行。

PHP 5.3.2-1ubuntu4.17 with Suhosin-Patch (cli) (built: Jun 19 2012 01:35:33)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with XCache v1.3.0, Copyright (c) 2005-2009, by mOo

Server version: Apache/2.2.14 (Ubuntu)
Server built:   Feb 14 2012 18:09:18

2 个答案:

答案 0 :(得分:4)

很多DateTime错误都是fixed since 5.3.2 我在5.3.16上测试了你的代码,它工作正常,没有重复的减法。

答案 1 :(得分:-1)

带反斜杠的是什么?至于远,我可以看到一切看起来都正确。