dev.vitelline.net

php: Comparing two equal dates gives a “false” result

or DateTime createFromFormat without time

Problem: I’ve recently had a bug comparing two equal dates in php code. Both were created using DateTime::createFromFormat(“m-d-Y”, $mydate). Cause: createFromFormat function with my parameters created DateTime objects with current (at the moment of creation) time. So, I got two dates at different times. As a result, the comparison operation worked badly for me, giving false. Solution: The great solution for this to use ! in front of my format, to have all-time parts set to 0, as here:

$sub_date = DateTime::createFromFormat('!m-d-Y', $submissions[$x]["answers"][$pos]["prettyFormat"]);

Leave a Reply

Your email address will not be published. Required fields are marked *