我有一个使用DateTime::createFromFormat
的函数,它产生了一些非常奇怪的行为。它正在改变解析后的字符串。时区以date_default_timezone_set('America/Chicago');
声明。它运行在Ubuntu 12.04上,Apache 2.2.22和PHP 5.4.25。功能是:
public function getTimeDiff()
{
\Zend\Debug\Debug::dump($this->start);
$startDateTime = \DateTime::createFromFormat('Y-m-d H:m:s.u',$this->start);
$startTimestamp = $startDateTime -> getTimestamp();
\Zend\Debug\Debug::dump($startDateTime);
\Zend\Debug\Debug::dump($this->stop);
$stopDateTime = \DateTime::createFromFormat('Y-m-d H:m:s.u',$this->stop);
$stopTimestamp = $stopDateTime -> getTimestamp();
\Zend\Debug\Debug::dump($stopDateTime);
$diffInSeconds = $stopTimestamp - $startTimestamp;
return $diffInSeconds;
}
调试信息如下所示:
string '2014-05-02 08:00:00.000' (length=23)
object(DateTime)[426]
public 'date' => string '2013-12-02 08:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 17:00:00.000' (length=23)
object(DateTime)[427]
public 'date' => string '2013-12-02 17:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 08:30:00.000' (length=23)
object(DateTime)[438]
public 'date' => string '2016-06-02 08:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 09:20:00.000' (length=23)
object(DateTime)[437]
public 'date' => string '2015-08-02 09:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 09:30:00.000' (length=23)
object(DateTime)[437]
public 'date' => string '2016-06-02 09:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 10:20:00.000' (length=23)
object(DateTime)[438]
public 'date' => string '2015-08-02 10:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 10:30:00.000' (length=23)
object(DateTime)[438]
public 'date' => string '2016-06-02 10:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 11:20:00.000' (length=23)
object(DateTime)[437]
public 'date' => string '2015-08-02 11:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 11:30:00.000' (length=23)
object(DateTime)[437]
public 'date' => string '2016-06-02 11:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 12:20:00.000' (length=23)
object(DateTime)[438]
public 'date' => string '2015-08-02 12:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 13:30:00.000' (length=23)
object(DateTime)[438]
public 'date' => string '2016-06-02 13:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
string '2014-05-02 14:20:00.000' (length=23)
object(DateTime)[437]
public 'date' => string '2015-08-02 14:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/Chicago' (length=15)
知道造成这种变化的原因是什么吗?我希望它显而易见,无论出于何种原因我都无法看到。
答案 0 :(得分:0)
在评论中提供了答案,并在此重申了一个简单的查找。
您的格式出错:Y-m-d H:m:s.u您将m用作月份和分钟。使用我几分钟。 -phpisuber01