找到你要的答案

Q:PHP DateTime Comparison

Q:PHP日期时间比较

I've become accostomed to using the identical comparison operator in PHP (===), instead of the equal comparison operator (==). I ran into an issue while using php's built in DateTime objects. I'm confused as to why the equal comparison return true, but the identical comparison return false in the following code?

Code:

$test1 = new DateTime("now");       // What I thought were identical objects
$test2 = new DateTime("now");       // 
$test3 = new DateTime("tomorrow");

echo("test1: ");
var_dump($test1);
echo("<br/>test2: ");
var_dump($test2);

echo("now === now: ");
var_dump($test1 === $test2);

echo("<br/>now == now: ");
var_dump($test1 == $test2);

echo("<br/>now < now: ");
var_dump($test1 < $test2);

echo("<br/>now > now: ");
var_dump($test1 > $test2);

echo("<br/>now < tomorrow: ");
var_dump($test2 < $test3);

echo("<br/>now > tomorrow: ");
var_dump($test2 > $test3);

Output:

    test1: object(DateTime)#36 (3) { ["date"]=> string(19) "2015-06-23 14:44:25" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "America/Chicago" } 
    test2: object(DateTime)#37 (3) { ["date"]=> string(19) "2015-06-23 14:44:25" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "America/Chicago" } 
    now === now: bool(false) 
    now == now: bool(true) 
    now < now: bool(false) 
    now > now: bool(false) 
    now < tomorrow: bool(true) 
    now > tomorrow: bool(false)

我已经accostomed在PHP中使用相同的比较运算符(= = =),而不是平等的比较运算符(= =)。我遇到了一个问题,虽然使用了PHP的DateTime对象。我很困惑为什么相等的比较返回true,但相同的比较返回FALSE在下面的代码?

代码:

$test1 = new DateTime("now");       // What I thought were identical objects
$test2 = new DateTime("now");       // 
$test3 = new DateTime("tomorrow");

echo("test1: ");
var_dump($test1);
echo("<br/>test2: ");
var_dump($test2);

echo("now === now: ");
var_dump($test1 === $test2);

echo("<br/>now == now: ");
var_dump($test1 == $test2);

echo("<br/>now < now: ");
var_dump($test1 < $test2);

echo("<br/>now > now: ");
var_dump($test1 > $test2);

echo("<br/>now < tomorrow: ");
var_dump($test2 < $test3);

echo("<br/>now > tomorrow: ");
var_dump($test2 > $test3);

输出:

    test1: object(DateTime)#36 (3) { ["date"]=> string(19) "2015-06-23 14:44:25" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "America/Chicago" } 
    test2: object(DateTime)#37 (3) { ["date"]=> string(19) "2015-06-23 14:44:25" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "America/Chicago" } 
    now === now: bool(false) 
    now == now: bool(true) 
    now < now: bool(false) 
    now > now: bool(false) 
    now < tomorrow: bool(true) 
    now > tomorrow: bool(false)
answer1: 回答1:

In case of object comparision === not only check value and object type but it will check references also. That's why in your case === returns false because of two separate instance.

Just to clarify check this out:-

https://eval.in/386378

Note:- In the first case two separate instances are there $test1 and $test2, that's why === returns false even object type and there values are equal.

But in the second case since $test1 and $test2 are same references so it states true.

Also in case of normal variables === check only value and datatype. where as == always checks values only and whenever datatype will different it will not give correct output. So be careful when using ==. Thanks.

如果对象比较= =不仅检查值和对象类型,但它也将检查引用。这就是为什么在你的情况下= =返回FALSE,因为两个独立的实例。

只是为了澄清这一点:—

https://eval.in/386378

注:在第一种情况下分别情况有$ test1和test2美元,这就是为什么= = =返回false,即使对象类型有值相等。

但是在第二种情况下因为test1和test2美元美元相同参考所以美国的真实。

也在正常的变量= = =只检查值和数据类型的情况下。而= =总是检查值只有当将不同数据类型不正确的输出。所以在使用时要小心=。谢谢.

answer2: 回答2:

Its quite clear to me that you are initializing test1 and test2 on two different lines. Most likeley they return different values and hence comparison will return false.

However the real issue is Identical Operator compares object .. and returns false if the object are different [that is referenced to different objects].

<?php

class Foo{}

$Foo1 = new Foo();
$Foo2 = new Foo();

echo ("Foo1 === Foo2");
var_dump($Foo1 === $Foo2);

Result in ::

Foo1 === Foo2bool(false)

它很清楚我你初始化test1,test2在两个不同的线。最likeley他们返回不同的值,因此比较将返回false。

然而真正的问题是相同的操作符比较对象…并返回FALSE,如果对象是不同的[被引用到不同的对象]。

<?php

class Foo{}

$Foo1 = new Foo();
$Foo2 = new Foo();

echo ("Foo1 === Foo2");
var_dump($Foo1 === $Foo2);

导致::

Foo1 === Foo2bool(false)
answer3: 回答3:

if you create the variables in sequence the result must be"false" because the datetime('now') in php returns the time at the moment so the seconds change for each variable however the problem could be because not set this parameter date_default_timezone_set('America/New_York');

    php > var_dump($test1);
       object(DateTime)#1 (3) {
       ["date"]=>
       string(26) "2015-06-23 15:46:34.000000"
       ["timezone_type"]=>
       int(3)
       ["timezone"]=>
       string(16) "America/New_York"
     }
     php > var_dump($test2);
     object(DateTime)#3 (3) {
     ["date"]=>
     string(26) "2015-06-23 15:46:38.000000"
     ["timezone_type"]=>
    int(3)
    ["timezone"]=>
     string(16) "America/New_York"
    }

如果你创建的变量序列的结果一定是“假”因为datetime(现在的)在PHP中返回的时候,改变每个变量但问题可能是因为没有设置这个参数date_default_timezone_set秒('america / new_york”);

    php > var_dump($test1);
       object(DateTime)#1 (3) {
       ["date"]=>
       string(26) "2015-06-23 15:46:34.000000"
       ["timezone_type"]=>
       int(3)
       ["timezone"]=>
       string(16) "America/New_York"
     }
     php > var_dump($test2);
     object(DateTime)#3 (3) {
     ["date"]=>
     string(26) "2015-06-23 15:46:38.000000"
     ["timezone_type"]=>
    int(3)
    ["timezone"]=>
     string(16) "America/New_York"
    }
php  datetime  comparison