首页 > php教程 > php手册 > 正文

php中日期时间比较三个函数

WBOY
发布: 2016-06-13 10:00:53
原创
833 人浏览过

文章介绍了三种常用的日期时间比较格式的函数,一个是对整日期,一个是只对时间比较,最后一个是专业比较时间差可以到秒。

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

复制代码 函数compare_date_time( $DATE_TIME1, $DATE_TIME2 )
{
        if ( $DATE_TIME1 == NULL || strlen( $DATE_TIME1 ) == 0 || $DATE_TIME2 == NULL || strlen( $DATE_TIME2 ) == 0 )
        {
                返回-1;
        }
        $DATE_TIME1_ARRY = 爆炸( " ", $DATE_TIME1 );
        $DATE_TIME2_ARRY = 爆炸( " ", $DATE_TIME2 );
        if (compare_date( $DATE_TIME1_ARRY[0], $DATE_TIME2_ARRY[0] ) == 1 )
        {
                返回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 )
                {
                        返回1;
                }
                if (compare_time( $DATE_TIME1_ARRY[1], $DATE_TIME2_ARRY[1] ) == 0 )
                {
                        返回0;
                }
                返回-1;
        }
        返回-1;
}
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板