小编Ser*_*der的帖子

PHP 日期时间比较

我已经习惯于在 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); …
Run Code Online (Sandbox Code Playgroud)

php comparison datetime

5
推荐指数
1
解决办法
2159
查看次数

标签 统计

comparison ×1

datetime ×1

php ×1