程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php中日期時間比較三個函數

php中日期時間比較三個函數

編輯:關於PHP編程

文章介紹了三種常用的日期時間比較格式的函數,一個是對整日期,一個是只對時間比較,最後一個是專業比較時間差可以到秒。

日期比較如 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;
}

格式: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

 代碼如下 復制代碼

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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved