简体   繁体   English

为什么在“YYYY-MM-DD”的日期格式中,两个字符串的PHP中的比较小于或者多于比较,即使它们是字符串?

[英]Why does a less than or more than comparison in PHP of two strings in the date format of “YYYY-MM-DD” work even though they are strings?

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. 我正在为一个项目的PHP代码部分工作,该项目将YYYY-MM-DD格式的日期与当前日期进行比较,以查看它是否小于当前日期。 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. 第一个在日期上使用get_timestamp()并运行时间戳的比较。 In another place it just compared the string of the date to the output from date("Ymd") . 在另一个地方,它只是将日期的字符串与日期的输出date("Ymd") 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. 我回顾了PHP手册的两个部分以获得洞察力,但我仍然感到困惑的是,为什么比较无需转换为时间戳。

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. PHP手册的比较运算符部分指出,字符串要么被转换为数字,要么进行数字比较,或者它产生一种称为词法比较的东西。 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() ? 哪个是用于将日期转换为时间戳的更好的函数: get_timestamp()strtotime()

Sample code of less than / more than comparison of two YYYY-MM-DD strings: 示例代码小于/大于两个YYYY-MM-DD字符串的比较:

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 示例代码测试转换后的YYYY-MM-DD字符串的数值

$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

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. 当你用这种方式比较字符串时,它将从左到右,并比较给定字符串中的每个字符以查看它们是否不同,直到找到差异,然后它将通过比较ASCII值来决定哪个字符串更大最后一个角色。 Coincidentally, since you are using only numbers, the highest numbers are also higher on the ASCII table. 巧合的是,由于您只使用数字,因此ASCII表上的最高数字也更高。

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. 另请注意,这仅适用于您使用YYYY-MM-DD格式,如果您使用其他格式则无法使用。

Use the DateTime class for comparing dates. 使用DateTime类比较日期。 It makes it a lot easier to understand, and you don't have to deal with type juggling. 这使得它更容易理解,你不必处理类型杂耍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PHP中比较格式为yyyy-mm-dd的日期字符串 - How to compare date strings in the format yyyy-mm-dd in PHP php比较PHP中的YYYY-mm-dd字符串 - php compare YYYY-mm-dd strings in php PHP 检查日期的正则表达式为 YYYY-MM-DD 格式 - PHP Regex to check date is in YYYY-MM-DD format 正则表达式使用格式为YYYY-MM-DD在PHP中验证日期 - Regex to validate date in PHP using format as YYYY-MM-DD 如何解析格式为“ yyyy-mm-dd到yyyy-mm-dd”的字符串范围以开始/结束日期 - How to parse a string range in the format “yyyy-mm-dd to yyyy-mm-dd” to start/end date 如何用PHP中“ Unix时间戳”格式的日期检查“ YYYY-MM-DD”格式的日期? - How to check the date in 'YYYY-MM-DD' format with the date present in 'Unix Time stamp' format in PHP? 在PHP中将“每月的日期YYYY”转换为YYYY-MM-DD - Convert “Day Date Of Month YYYY” To YYYY-MM-DD In Php 在en_US语言环境中使用Zend_Validate_Date验证YYYY-MM-DD格式的字符串 - Validating YYYY-MM-DD formatted strings with Zend_Validate_Date in en_US locale 在laravel中以mysql date格式插入日期为yyyy-mm-dd - Insert a date with format of mysql date as yyyy-mm-dd in laravel 在laravel刀片中将引导日期格式设置为yyyy-mm-dd - Setting bootstrap date format to yyyy-mm-dd in laravel blade
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM