Home >Backend Development >PHP Tutorial >Can strcmp compare two strings in time format?

Can strcmp compare two strings in time format?

WBOY
WBOYOriginal
2016-09-29 09:33:011929browse

strcmp("2016-09-27","2016-09-26") The result is greater than 0, but can all time formats be compared in this way?

Reply content:

strcmp("2016-09-27","2016-09-26") The result is greater than 0, but can all time formats be compared in this way?

If your purpose is just to compare, you can compare like this

It has nothing to do with the time format, but the numeric time format can actually be compared like this, but the date format must be the same

<code>echo strcmp("2016-09-27","2016-9-26"); // -1
echo strcmp("2016-11-27","2016-9-26"); // -1
echo strcmp("2016-09-27","2016-09-26");// 1
echo strcmp("2016-10-01","2016-09-26");// 1</code>

strcmp first compares the string length. If the string length of A is greater than B, then it is considered that A is greater than B.
If the lengths of the strings are equal, compare the "lexicographic order" of each character one by one. The later words are greater than the earlier words. Until one of the words in the former is greater or less than the latter, the string is considered to be greater or less than the latter. By.
"2016-09-27" and "2016-09-26", compare the character sizes one by one until '7' > '6'(ascii), it is considered that 2016-09-27 is greater than 2016 -09-26, although the same time format can be compared with strcmp, is essentially a comparison of strings, because the size of the later time string is always larger than the earlier time string.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn