why this two dates doesn't equal?
<?php
$a = new DateTime();
$b = new DateTime();
var_dump($a->format('d-m-Y H:i:s'));
var_dump($b->format('d-m-Y H:i:s'));
if ($a == $b) {
echo "same";
}
i try PHP 8.1, 8.0 and 7.4 so i don't think is a bug, maybe DateTime compare microseconds? If that's so how i compare simple d-m-Y? Wihtout simply formatting and do a string comparison of course
CodePudding user response:
Yeah there is a .000003 delay between each line of PHP hence == wont work because it's not exactly the same
Example:
<?php
$a = new DateTime();
$b = new DateTime();
var_dump($a);
echo "<br>";
var_dump($b);
Output:
object(DateTime)#1 (3) { ["date"]=> string(26) "2022-06-19 15:26:56.020431" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" }
object(DateTime)#2 (3) { ["date"]=> string(26) "2022-06-19 15:26:56.020434" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" }```