The article introduces three kinds of commonly used date time comparison format function, one is to the whole date, one is only to the time comparison, the last one is the professional comparison difference can be to seconds.
Date comparison as of 2011-11-11 2011-12-12
| The code is as follows |
Copy Code |
function Compare_date ($DATE 1, $DATE 2) { $STR = Strtok ($DATE 1, "-"); $YEAR 1 = $STR; $STR = Strtok ("-"); $MON 1 = $STR; $STR = Strtok ("-"); $DAY 1 = $STR; $STR = Strtok ($DATE 2, "-"); $YEAR 2 = $STR; $STR = Strtok ("-"); $MON 2 = $STR; $STR = Strtok ("-"); $DAY 2 = $STR; if ($YEAR 2 < $YEAR 1) { return 1; } if ($YEAR 1 < $YEAR 2) { return-1; } if ($MON 2 < $MON 1) { return 1; } if ($MON 1 < $MON 2) { return-1; } if ($DAY 2 < $DAY 1) { return 1; } if ($DAY 1 < $DAY 2) { return-1; } return 0; } |
Format: 12.00-13.11
| code as follows |
copy code |
function Compare_time ($TIME 1, $TIME 2) { $STR = Strtok ($TIME 1, ":"); $HOUR 1 = $STR; $STR = Strtok (":"); $MIN 1 = $STR; $STR = Strtok (":"); $SEC 1 = $STR; $STR = Strtok ($TIME 2, ":"); $HOUR 2 = $STR; $STR = Strtok (":"); $MIN 2 = $STR; $STR = Strtok (":"); $SEC 2 = $STR; if ($HOUR 2 < $HOUR 1) { return 1; } if ($HOUR 1 < $HOUR 2) { return-1; } if ($MIN 2 < $MIN 1) { return 1; } if ($MIN 1 < $MIN 2) { return-1; } if ($SEC 2 < $SEC 1) { return 1; } if ($SEC 1 < $SEC 2) { return-1; } return 0; } |
Format: 2011-11-12 1:6:25, 2011-12-13 1:2:35
| code as follows |
copy code |
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; } |
http://www.bkjia.com/PHPjc/631661.html www.bkjia.com true http://www.bkjia.com/PHPjc/631661.html techarticle The article introduces three kinds of commonly used date time comparison format function, one is to the whole date, one is only to the time comparison, the last one is the professional comparison difference can be to seconds. Date comparison ...