2015-06-29 120 views
3

这是将DateTimeZone对象与给定时区进行比较的唯一方法吗?PHP比较DateTimeZone对象

$dateNow = new \DateTime('now'); 
    $tz = $dateNow->getTimezone(); 
    $this->assertEquals($tz->getName(), $tz->listIdentifiers(\DateTimeZone::UTC)[0]); 

既不比较两个对象也不比较常量。顺便说一下,DateTimeZone常量用于什么?

+1

请注意你的语法。语法不好的问题不太可能得到答案。我现在就为你解决这个问题。 – Dakkaron

回答

0

两个DateTimeZone对象可以通过它们各自的名称进行比较。

只使用DateTimeZone对象的一个​​例子:

$UTC = new DateTimeZone('UTC'); 
$UTC2 = new DateTimeZone('UTC'); 
$PST = new DateTimeZone('America/Los_Angeles'); 
if ($UTC->getName() == $PST->getName()) 
{ 
    echo "UTC equals PST"; 
} 
else 
{ 
    echo "UTC does not equal PST"; 
} 

if ($UTC->getName() == $UTC2->getName()) 
{ 
    echo "UTC equals UTC"; 
} 
else 
{ 
    echo "UTC does not equal UTC"; 
} 

使用datetime对象的一个​​例子:

​​