快捷搜索:   文件 java linux

PHP获取日期详细

/**
 * [getStartDateAndEndDate PHP获取日期详细获取开始时间与结束时间的差额天数]
 * @param  [type] $startTime [description]
 * @param  [type] $EndTime   [description]
 * @return [type]            [description]
 */
function getStartDateAndEndDate($StartTime,$EndTime,$DateType = 1)
{
    //开始时间的时间戳
    $StartTimeStamp = strtotime($StartTime);
    //获取开始时间的年份
    $StartTimeYear = date('Y', $StartTimeStamp);
    //获取开始时间的月份 12
    $StartTimeMonth = date('m',$StartTimeStamp);
    //获取开始时间的日期 03
    $StartTimeDay = date('d', $StartTimeStamp);
    //获取开始时间的月份天数 31
    $StartTimeDayNum = date('t',$StartTimeStamp);
    //结束时间的时间戳
    $EndTimeStamp = strtotime($EndTime);
    //获取结束时间的年份
    $EndTimeYear = date('Y', $EndTimeStamp);
    //获取结束时间的月份 12
    $EndTimeMonth = date('m',$EndTimeStamp);
    //获取结束时间的日期 25
    $EndTimeDay = date('d', $EndTimeStamp);
    //获取结束时间的月份天数 31
    $EndTimeDayNum = date('t', $$EndTimeStamp);
    //获取开始年份与结束年份总差年
    for($i=($StartTimeYear+1);$i<$EndTimeYear;$i++)
    {
        $YearAllMonthNum[] = $i;
    }
    //获取开始年到年底的月份
    $StartTimeMothNul = 12 - $StartTimeMonth;
    //获取结束年到开始到结束的月份
    $EndTimeMothNul = $EndTimeMonth - 1;
    
    //计算开始时间与结束时间相差几年
    $StartAndEndYear = ($EndTimeYear-$StartTimeYear);
    //计算开始时间与结束时间相差几月
    $StartAndEndMonth = empty($YearAllMonthNum) ? $EndTimeMonth - $StartTimeMonth : (count($YearAllMonthNum) * 12) + $StartTimeMothNul + $EndTimeMothNul;
    
    //结束时间减去开始时间为  两个时间相差的天数
    $StartAndEndDay = floor(($EndTimeStamp -$StartTimeStamp)/86400) < 0 ? 0 : floor(($EndTimeStamp -$StartTimeStamp)/86400);
    $data['StartAndEndDay'] = $StartAndEndDay;
    $data['StartAndEndMonth'] = $StartAndEndMonth;
    $data['StartAndEndYear'] = $StartAndEndYear;
    $data['StartTimeMothNul'] = $StartTimeMothNul;
    $data['EndTimeMothNul'] = $EndTimeMothNul;
    $data['StartTimeStamp'] = $StartTimeStamp;
    $data['StartTimeYear'] = $StartTimeYear;
    $data['StartTimeMonth'] = $StartTimeMonth;
    $data['StartTimeDay'] = $StartTimeDay;
    $data['StartTimeDayNum'] = $StartTimeDayNum;
    $data['EndTimeStamp'] = $EndTimeStamp;
    $data['EndTimeYear'] = $EndTimeYear;
    $data['EndTimeMonth'] = $EndTimeMonth;
    $data['EndTimeDay'] = $EndTimeDay;
    $data['EndTimeDayNum'] = $EndTimeDayNum;
    
    //判断返回类型
    if($DateType == 1)
    {
        //循环年
        for($y=$StartTimeYear;$y<=$EndTimeYear;$y++)
        {   //判断是否为当前年
            if($y == $StartTimeYear)
            {
                //循环月
                for($m=intval($StartTimeMonth);$m<=12;$m++)
                {
                    $mNum = strlen($m)==1? '0'.$m : $m ;
                    if($y.$mNum <= $EndTimeYear.$EndTimeMonth){
                        if(strlen($m) == 1){
                            //判断是否为当前月
                            if($m == $StartTimeMonth){
                                //判断结束天是哪天
                                if($StartTimeMonth == $EndTimeMonth){
                                    //循环日
                                    for($d=intval($StartTimeDay);$d<=intval($EndTimeDay);$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][$y][$m][] = $y . '-0' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][$y][$m][] = $y . '-0' . $m . '-' . $d;
                                        }
                                    }
                                }else{
                                    for($d=intval($StartTimeDay);$d<=date('t',strtotime($y.'-'.$m));$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][$y][$m][] = $y . '-0' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][$y][$m][] = $y . '-0' . $m . '-' . $d;
                                        }
                                    }
                                }
                            }else{
                                if($y.$m == $EndTimeYear.intval($EndTimeMonth)){
                                    for($d=1;$d<=intval($EndTimeDay);$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][$y][$m][] = $y . '-0' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][$y][$m][] = $y . '-0' . $m . '-' . $d;
                                        }
                                    }
                                }else{
                                    for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][$y][$m][] = $y . '-0' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][$y][$m][] = $y . '-0' . $m . '-' . $d;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            //判断是否为当前月
                            if($m == $StartTimeMonth){
                                //判断结束天是哪天
                                if($StartTimeMonth == $EndTimeMonth){
                                    //循环日
                                    for($d=intval($StartTimeDay);$d<=intval($EndTimeDay);$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][$y][$m][] = $y . '-' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][$y][$m][] = $y . '-' . $m . '-' . $d;
                                        }
                                    }
                                }else{
                                    for($d=intval($StartTimeDay);$d<=date('t',strtotime($y.'-'.$m));$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][$y][$m][] = $y . '-' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][$y][$m][] = $y . '-' . $m . '-' . $d;
                                        }
                                    }
                                }
                            }else{
                                if($y.$m == $EndTimeYear.intval($EndTimeMonth)){
                                    for($d=1;$d<=intval($EndTimeDay);$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][$y][$m][] = $y . '-' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][$y][$m][] = $y . '-' . $m . '-' . $d;
                                        }
                                    }
                                }else{
                                    for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][$y][$m][] = $y . '-' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][$y][$m][] = $y . '-' . $m . '-' . $d;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if($y == $EndTimeYear)
            {
                //循环月
                for($m=1;$m<=$EndTimeMonth;$m++)
                {
                    if(strlen($m) == 1){
                        //循环日
                        if($m == $EndTimeMonth){
                            for($d=1;$d<=intval($EndTimeDay);$d++)
                            {
                                if(strlen($d) == 1){
                                    $data['date'][$y][$m][] = $y . '-0' . $m . '-0' . $d;
                                }else{
                                    $data['date'][$y][$m][] = $y . '-0' . $m . '-' . $d;
                                }
                            }
                        }else{
                            for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                            {
                                if(strlen($d) == 1){
                                    $data['date'][$y][$m][] = $y . '-0' . $m . '-0' . $d;
                                }else{
                                    $data['date'][$y][$m][] = $y . '-0' . $m . '-' . $d;
                                }
                            }
                        }
                    }
                    else
                    {
                        if($m == $EndTimeMonth){
                            for($d=1;$d<=intval($EndTimeDay);$d++)
                            {
                                if(strlen($d) == 1){
                                    $data['date'][$y][$m][] = $y . '-' . $m . '-0' . $d;
                                }else{
                                    $data['date'][$y][$m][] = $y . '-' . $m . '-' . $d;
                                }
                            }
                        }else{
                            for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                            {
                                if(strlen($d) == 1){
                                    $data['date'][$y][$m][] = $y . '-' . $m . '-0' . $d;
                                }else{
                                    $data['date'][$y][$m][] = $y . '-' . $m . '-' . $d;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                //循环月
                for($m=1;$m<=12;$m++)
                {
                    if(strlen($m) == 1)
                    {
                        //循环日
                        for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                        {
                            if(strlen($d) == 1){
                                $data['date'][$y][$m][] = $y . '-0' . $m . '-0' . $d;
                            }else{
                                $data['date'][$y][$m][] = $y . '-0' . $m . '-' . $d;
                            }
                        }
                    }
                    else
                    {
                        //循环日
                        for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                        {
                            if(strlen($d) == 1){
                                $data['date'][$y][$m][] = $y . '-' . $m . '-0' . $d;
                            }else{
                                $data['date'][$y][$m][] = $y . '-' . $m . '-' . $d;
                            }
                        }
                    }
                }
            }
        }
    }
    //返回并排
    else if($DateType == 2)
    {  
        //循环年
        for($y=$StartTimeYear;$y<=$EndTimeYear;$y++)
        {   //判断是否为当前年
            if($y == $StartTimeYear)
            {
                //循环月
                for($m=intval($StartTimeMonth);$m<=12;$m++)
                {
                    $mNum = strlen($m)==1? '0'.$m : $m ;
                    if($y.$mNum <= $EndTimeYear.$EndTimeMonth){
                        if(strlen($m) == 1){
                            //判断是否为当前月
                            if($m == $StartTimeMonth){
                                //判断结束天是哪天
                                if($StartTimeMonth == $EndTimeMonth){
                                    //循环日
                                    for($d=intval($StartTimeDay);$d<=intval($EndTimeDay);$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][] = $y . '-0' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][] = $y . '-0' . $m . '-' . $d;
                                        }
                                    }
                                }else{
                                    for($d=intval($StartTimeDay);$d<=date('t',strtotime($y.'-'.$m));$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][] = $y . '-0' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][] = $y . '-0' . $m . '-' . $d;
                                        }
                                    }
                                }
                            }else{
                                if($y.$m == $EndTimeYear.intval($EndTimeMonth)){
                                    for($d=1;$d<=intval($EndTimeDay);$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][] = $y . '-0' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][] = $y . '-0' . $m . '-' . $d;
                                        }
                                    }
                                }else{
                                    for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][] = $y . '-0' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][] = $y . '-0' . $m . '-' . $d;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            //判断是否为当前月
                            if($m == $StartTimeMonth){
                                //判断结束天是哪天
                                if($StartTimeMonth == $EndTimeMonth){
                                    //循环日
                                    for($d=intval($StartTimeDay);$d<=intval($EndTimeDay);$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][] = $y . '-' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][] = $y . '-' . $m . '-' . $d;
                                        }
                                    }
                                }else{
                                    for($d=intval($StartTimeDay);$d<=date('t',strtotime($y.'-'.$m));$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][] = $y . '-' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][] = $y . '-' . $m . '-' . $d;
                                        }
                                    }
                                }
                            }else{
                                if($y.$m == $EndTimeYear.intval($EndTimeMonth)){
                                    for($d=1;$d<=intval($EndTimeDay);$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][] = $y . '-' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][] = $y . '-' . $m . '-' . $d;
                                        }
                                    }
                                }else{
                                    for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                                    {
                                        if(strlen($d) == 1){
                                            $data['date'][] = $y . '-' . $m . '-0' . $d;
                                        }else{
                                            $data['date'][] = $y . '-' . $m . '-' . $d;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if($y == $EndTimeYear)
            {
                //循环月
                for($m=1;$m<=$EndTimeMonth;$m++)
                {
                    if(strlen($m) == 1){
                        //循环日
                        if($m == $EndTimeMonth){
                            for($d=1;$d<=intval($EndTimeDay);$d++)
                            {
                                if(strlen($d) == 1){
                                    $data['date'][] = $y . '-0' . $m . '-0' . $d;
                                }else{
                                    $data['date'][] = $y . '-0' . $m . '-' . $d;
                                }
                            }
                        }else{
                            for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                            {
                                if(strlen($d) == 1){
                                    $data['date'][] = $y . '-0' . $m . '-0' . $d;
                                }else{
                                    $data['date'][] = $y . '-0' . $m . '-' . $d;
                                }
                            }
                        }
                    }
                    else
                    {
                        if($m == $EndTimeMonth){
                            for($d=1;$d<=intval($EndTimeDay);$d++)
                            {
                                if(strlen($d) == 1){
                                    $data['date'][] = $y . '-' . $m . '-0' . $d;
                                }else{
                                    $data['date'][] = $y . '-' . $m . '-' . $d;
                                }
                            }
                        }else{
                            for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                            {
                                if(strlen($d) == 1){
                                    $data['date'][] = $y . '-' . $m . '-0' . $d;
                                }else{
                                    $data['date'][] = $y . '-' . $m . '-' . $d;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                //循环月
                for($m=1;$m<=12;$m++)
                {
                    if(strlen($m) == 1)
                    {
                        //循环日
                        for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                        {
                            if(strlen($d) == 1){
                                $data['date'][] = $y . '-0' . $m . '-0' . $d;
                            }else{
                                $data['date'][] = $y . '-0' . $m . '-' . $d;
                            }
                        }
                    }
                    else
                    {
                        //循环日
                        for($d=1;$d<=date('t',strtotime($y.'-'.$m));$d++)
                        {
                            if(strlen($d) == 1){
                                $data['date'][] = $y . '-' . $m . '-0' . $d;
                            }else{
                                $data['date'][] = $y . '-' . $m . '-' . $d;
                            }
                        }
                    }
                }
            }
        }
    }
    else if($DateType == 3)
    {
        //返回统计类型
    }
    return $data;
}



您可能还会对下面的文章感兴趣:

头像:

设置头像

x
名称(*):
域名    :
邮箱    :

评论内容(*): 刷新评论 提交评论