search
HomeBackend DevelopmentPHP TutorialPHP date string comparison example

There is a function in the project that compares whether members have expired. I reviewed my colleague’s code and found that the writing method was rather strange, but there were no bugs online.

Achievement:

  1. $expireTime = "2014-05-01 00:00:00";
  2. $currentTime = date('Y-m-d H:i:s', time());
  3. if($currentTime return false;
  4. } else {
  5. return true;
  6. }
Copy code

If two times need to be compared, it is usually converted to a unix timestamp, using two int numbers. Compare. This implementation specifically expresses time as a string, and then performs a comparison operation on the two strings.

Writing aside, I am very curious about how comparison is performed internally in PHP.

Without further ado, let’s start tracking from the source code.

Compilation period Syntax similar to the following can be found in zend_language_parse.y: =expr === Expr {Zend_do_binary_op (ZEND_IS_IDENTICAL, & $ 1, & $ 3 TSRMLS_CC); Identical, & $ $, & $ 1, & $ 3 TSRMLS_CC ); }

expr == expr { zend_do_binary_op(ZEND_IS_EQUAL, &$$, &$1, &$3 TSRMLS_CC); }
expr != expr { zend_do_binary_op(ZEND_IS_NOT_EQUAL, &$$, &$1, &$3 TSRMLS_CC); }
expr expr expr > expr { zend_do_binary_op(ZEND_IS_SMALLER, &$$, &$3, &$1 TSRMLS_CC); }
  • expr >= expr { zend_do_binary_op(ZEND_IS_SMALLER_OR_EQUAL, &$$, &$3, &$1 TSRMLS_CC); }
  • Copy code
  • Obviously, zend_do_binary_op is used to compile opcode here.
  • void zend_do_binary_op(zend_uchar op, znode *result, const znode *op1, const znode *op2 TSRMLS_DC) /* {{{ */

    {

    zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC );
    opline->opcode = op;
      opline->result.op_type = IS_TMP_VAR;
    1. opline->result.u.var = get_temporary_variable(CG(active_op_array));
    2. opline->op1 = *op1;
    3. opline->op2 = *op2;
    4. *result = opline->result;
    5. }
    6. Copy code
    7. This function does not do any special processing, it just simply saves the opcode and operands 1 and operand 2.
    Execution period According to the opcode, jump to the corresponding processing function: ZEND_IS_SMALLER_SPEC_CONST_CONST_HANDLER.

    static int ZEND_FASTCALL ZEND_IS_SMALLER_SPEC_CONST_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)

    {

    zend_op *opline = EX(opline);
    zval *result = &EX_T(opline-> result.u.var).tmp_var;
    1. compare_function( result,
    2. &opline->op1.u.constant,
    3. &opline->op2.u.constant TSRMLS_CC);
    4. ZVAL_BOOL(result, (Z_LVAL_P(result)
    5. ZEND_VM_NEXT_OPCODE();
    6. }
    7. Copy the code
    8. Notice that the comparison of two zvals is handled by compare_function.
    ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */

    {

    int ret;
    int converted = 0;
    zval op1_copy, op2_copy;
      zval *op_free;
    1. while (1) {
    2. switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
    3. case TYPE_PAIR(IS_LONG, IS_LONG):
    4. ...
    5. case TYPE_PAIR(IS_DOUBLE, IS_LONG):
    6. ...
    7. case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
    8. ...
    9. ...
    10. // Compare two strings
    11. case TYPE_PAIR(IS_STRING, IS_STRING):
    12. zendi_smart_strcmp(result, op1, op2);
    13. return SUCCESS;
    14. ...
    15. }
    16. }
    17. }
    18. Copy code
    19. This function exemplifies several situations. According to the case in this article, enter zendi_smart_strcmp to get a closer look:

      1. ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2) /* {{{ */
      2. {
      3. int ret1, ret2;
      4. long lval1, lval2;
      5. double dval1, dval2;
      6. // Try to convert the string to a numeric type
      7. if ((ret1=is_numeric_string(Z_STRVAL_P(s1), Z_STRLEN_P(s1), &lval1, &dval1, 0)) &&
      8. (ret2=is_numeric_string(Z_STRVAL_P(s2), Z_STRLEN_P( s2), &lval2, &dval2, 0))) {
      9. // Compare numbers
      10. ...
      11. } else {
      12. // All cannot be converted into numbers
      13. // Then call zend_binary_zval_strcmp
      14. // Essentially memcmp One layer of encapsulation
      15. Z_LVAL_P(result) = zend_binary_zval_strcmp(s1, s2);
      16. ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(result)));
      17. }
      18. }
      Copy code

      Then "2014-05-01 Can 00:00:00" be converted into a number?

      You still have to look at the implementation rules of is_numeric_string.

      1. static inline zend_uchar is_numeric_string(const char *str, int length, long *lval, double *dval, int allow_errors)
      2. {
      3. const char *ptr;
      4. int base = 10, digits = 0, dp_or_e = 0;
      5. double local_dval;
      6. zend_uchar type;
      7. if (!length) {
      8. return 0;
      9. }
      10. /* trim out the blank part at the beginning of the string*/
      11. while (*str == ' ' || * str == 't' || *str == 'n' || *str == 'r' || *str == 'v' || *str == 'f') {
      12. str++;
      13. length- -;
      14. }
      15. ptr = str;
      16. if (*ptr == '-' || *ptr == '+') {
      17. ptr++;
      18. }
      19. if (ZEND_IS_DIGIT(*ptr)) {
      20. /* Determine whether it is hexadecimal */
      21. if (length > 2 && *str == '0' && (str[1] == 'x' || str[1] == 'X')) {
      22. base = 16;
      23. ptr += 2;
      24. }
      25. /* Ignore the following 0s */
      26. while (*ptr == '0') {
      27. ptr++;
      28. }
      29. /* Calculate the number of digits in the number, and Determine whether it is integer or floating point*/
      30. for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors == 1)); digits++, ptr++) {
      31. check_digits:
      32. if (ZEND_IS_DIGIT(*ptr ) || (base == 16 && ZEND_IS_XDIGIT(*ptr))) {
      33. continue;
      34. } else if (base == 10) {
      35. if (*ptr == '.' && dp_or_e goto process_double;
      36. } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e const char *e = ptr + 1;
      37. if (*e == '-' || *e == '+') {
      38. ptr = e++;
      39. }
      40. if (ZEND_IS_DIGIT(*e)) {
      41. goto process_double;
      42. }
      43. }
      44. }
      45. break;
      46. }
      47. if (base == 10) {
      48. if (digits >= MAX_LENGTH_OF_LONG) {
      49. dp_or_e = -1;
      50. goto process_double;
      51. }
      52. } else if (!(digits if (dval) {
      53. local_dval = zend_hex_strtod(str, (char **)&ptr);
      54. }
      55. type = IS_DOUBLE;
      56. }
      57. } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
      58. // Process floating point numbers
      59. } else {
      60. return 0;
      61. }
      62. // If fault tolerance is not allowed, exit with an error
      63. if ( ptr != str + length) {
      64. if (!allow_errors) {
      65. return 0;
      66. }
      67. if (allow_errors == -1) {
      68. zend_error(E_NOTICE, "A non well formed numeric value encountered");
      69. }
      70. }
      71. // To allow fault tolerance, try to convert str into numbers
      72. if (type == IS_LONG) {
      73. if (digits == MAX_LENGTH_OF_LONG - 1) {
      74. int cmp = strcmp(&ptr[-digits], long_min_digits);
      75. if (!(cmp if (dval) {
      76. *dval = zend_strtod(str, NULL);
      77. }
      78. return IS_DOUBLE;
      79. }
      80. }
      81. if (lval) {
      82. *lval = strtol(str, NULL, base);
      83. }
      84. return IS_LONG;
      85. } else {
      86. if (dval) {
      87. *dval = local_dval;
      88. }
      89. return IS_DOUBLE;
      90. }
      91. }
      Copy code

      The code is relatively long, but if you read it carefully, the rules for converting str to num are still very clear.

      Pay special attention to the allow_errors parameter, which directly determines that "2014-05-01 00:00:00" cannot be converted into a number in this example.

      So in the end, the operation of "2014-04-17 00:00:00"

      Since it is memcmp, it is not difficult to understand why the writing method mentioned at the beginning of the article can also run correctly.

      Fault-tolerant conversion When is allow_errors true? An excellent example is zend_parse_parameters. The implementation of zend_parse_parameters will not be described in detail. Interested readers can study it by themselves. When calling is_numeric_string, allow_errors is set to -1.

      For example:

      1. static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime)
      2. {
      3. char *format;
      4. int format_len;
      5. long ts;
      6. char *string;
      7. // The expected second parameter is timestamp, which is long
      8. // Assuming that the string is mistakenly passed in when the upper layer is called, zend_parse_parameters will still try to parse the string into long as much as possible
      9. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &format, &format_len, &ts) = = FAILURE) {
      10. RETURN_FALSE;
      11. }
      12. if (ZEND_NUM_ARGS() == 1) {
      13. ts = time(NULL);
      14. }
      15. string = php_format_date(format, format_len, ts, localtime TSRMLS_CC);
      16. RETVAL_STRING( string, 0);
      17. }
      Copy code

      This is the internal implementation of PHP's date function.

      When calling date, if the second parameter is passed into string, the effect is as follows:

      1. echo date('Y-m-d', '0-1-2');
      2. // Output
      3. PHP Notice: A non well formed numeric value encountered in Command line code on line 1
      4. 1970- 01-01
      Copy code

      Although a notice level error was reported, '0-1-2' was still successfully converted to 0



    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
    The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

    What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

    PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

    PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

    PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

    PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

    PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools