The date and time comparison functions in php. This article introduces three commonly used date and time comparison functions, one is for the whole date, the other is for the time comparison only, and the last is for the professional comparison time difference to seconds. The date comparison document introduces three commonly used date and time comparison formats. one is for the whole date, the other is for the time comparison only, and the last is for the professional comparison time difference to seconds.
The date comparison is shown in 2011-11-11 2011-12-12.
The code is as follows: |
|
Function compare_date ($ DATE1, $ DATE2) { $ STR = strtok ($ DATE1 ,"-"); $ YEAR1 = $ STR; $ STR = strtok ("-"); $ MON1 = $ STR; $ STR = strtok ("-"); $ DAY1 = $ STR; $ STR = strtok ($ DATE2 ,"-"); $ YEAR2 = $ STR; $ STR = strtok ("-"); $ MON2 = $ STR; $ STR = strtok ("-"); $ DAY2 = $ STR; If ($ YEAR2 <$ YEAR1) { Return 1; } If ($ YEAR1 <$ YEAR2) { Return-1; } If ($ MON2 <$ MON1) { Return 1; } If ($ MON1 <$ MON2) { Return-1; } If ($ DAY2 <$ DAY1) { Return 1; } If ($ DAY1 <$ DAY2) { Return-1; } Return 0; } |
Format: 12.00-13.11
The code is as follows: |
|
Function compare_time ($ TIME1, $ TIME2) { $ STR = strtok ($ TIME1 ,":"); $ HOUR1 = $ STR; $ STR = strtok (":"); $ MIN1 = $ STR; $ STR = strtok (":"); $ SEC1 = $ STR; $ STR = strtok ($ TIME2 ,":"); $ HOUR2 = $ STR; $ STR = strtok (":"); $ MIN2 = $ STR; $ STR = strtok (":"); $ SEC2 = $ STR; If ($ HOUR2 <$ HOUR1) { Return 1; } If ($ HOUR1 <$ HOUR2) { Return-1; } If ($ MIN2 <$ MIN1) { Return 1; } If ($ MIN1 <$ MIN2) { Return-1; } If ($ SEC2 <$ SEC1) { Return 1; } If ($ SEC1 <$ SEC2) { Return-1; } Return 0; } |
Format :,
The code is as follows: |
|
Function compare_date_time ($ DATE_TIME1, $ DATE_TIME2) { If ($ DATE_TIME1 = NULL | strlen ($ DATE_TIME1) = 0 | $ DATE_TIME2 = NULL | strlen ($ DATE_TIME2) = 0) { Return-1; } $ DATE_TIME1_ARRY = explode ("", $ DATE_TIME1 ); $ DATE_TIME2_ARRY = explode ("", $ DATE_TIME2 ); If (compare_date ($ DATE_TIME1_ARRY [0], $ DATE_TIME2_ARRY [0]) = 1) { Return 1; } If (compare_date ($ DATE_TIME1_ARRY [0], $ DATE_TIME2_ARRY [0]) = 0) { If (compare_time ($ DATE_TIME1_ARRY [1], $ DATE_TIME2_ARRY [1]) = 1) { Return 1; } If (compare_time ($ DATE_TIME1_ARRY [1], $ DATE_TIME2_ARRY [1]) = 0) { Return 0; } Return-1; } Return-1; } |
Bytes. Date comparison...