The article introduces three commonly used date and time comparison format functions, one is to compare the whole date, the other is to compare only the time, and the last one is to professionally compare the time difference to seconds.
Date comparison is like 2011-11-11 2011-12-12
代码如下 | 复制代码 |
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
代码如下 | 复制代码 |
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; } |
格式:2011-11-12 1:6:25 ,2011-12-13 1:2:35
代码如下
|
复制代码 | ||||
http://www.bkjia.com/PHPjc/631661.html