I am working on a section of PHP code for a project that compares a date in the YYYY-MM-DD format to the current date to see if it is less than the current date. At different points in the code two different methods were used for making this comparison. The first used get_timestamp()
on the dates and ran the comparison off of the timestamps. In another place it just compared the string of the date to the output from date("Y-m-d")
. My expectation was that the comparison of two date strings would not provide a correct response. However, when I set up several test cases I got the output expected. I reviewed two parts of the PHP manual for insight and I am still confused as to why the comparison works without conversion to a timestamp.
The comparison operators section of the PHP manual states that the strings are either being converted to numbers and a numerical comparison is then made or that it makes something called a lexical comparison. I couldn't find anything further on lexical comparisons, but when I read about string conversion to numbers and tried the examples using date strings I get the same numerical output for each date string I try.
Can someone help me understand why a greater than or less than comparison of two date strings works (or at least appears to be working)? What am I missing?
As a follow up, if it really does work, I am assuming it is a better practice to convert the date to a timestamp and do the comparison based on the timestamp. Which is the better function to use for converting a date to a timestamp: get_timestamp()
or strtotime()
?
Sample code of less than / more than comparison of two YYYY-MM-DD strings:
if ("2013-06-27" < "2013-06-28") { echo "less"; } // Output: less
if ("2013-06-27" > "2013-06-28") { echo "more"; } // Output: (none)
if ("2013-06-29" < "2013-06-28") { echo "less"; } // Output: (none)
if ("2013-06-29" > "2013-06-28") { echo "more"; } // Output: more
Sample code testing numeric value of a converted YYYY-MM-DD string
$foo = 1 + "2013-06-27";
echo "\$foo==$foo"; // Output: $foo = 2014
$foo = 1 + "2013-06-28";
echo "\$foo==$foo"; // Output: $foo = 2014
$foo = 1 + "2013-06-29";
echo "\$foo==$foo"; // Output: $foo = 2014
The validateDate() function checks whether the given string is a valid date using PHP. It uses PHP DateTime class to validate date based on the specified format. This function returns TRUE if date string is valid, otherwise FALSE.
php $date_now = time(); //current timestamp $date_convert = strtotime('2022-08-01'); if ($date_now > $date_convert) { echo 'greater than'; } else { echo 'Less than'; } ?>
PHP will compare alpha strings using the greater than and less than comparison operators based upon alphabetical order. In the first example, ai comes before i in alphabetical order so the test of > (greater than) is false - earlier in the order is considered 'less than' rather than 'greater than'.
PHP strcmp() Function: The strcmp() is an inbuilt function in PHP that is used to compare two strings. This function is case-sensitive which points that capital and small cases will be treated differently, during comparison.
When you compare a string this way, it will go from left to right, and compare each character in the given string to see if they are different, until it finds a difference, then it will decide which string is bigger by comparing the ASCII value of this last character. Coincidentally, since you are using only numbers, the highest numbers are also higher on the ASCII table.
This solution will work as long as you use only numbers in your comparisons, and that each string has the same number of characters
Also note that this works only since you are using the YYYY-MM-DD format, if you used another format it would not work.
Use the DateTime
class for comparing dates. It makes it a lot easier to understand, and you don't have to deal with type juggling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With