Discussion: How to use PHP to calculate the number of years months weeks and days between two dates

  • 2020-06-15 07:53:22
  • OfStack

PHP is used to calculate the number of years, months, weeks and days between two dates:


<?php
    function format($a,$b){
        // Check the two date sizes, default small before and large after, and switch positions if large before and small after to ensure large before and large after 
        if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a);
        $start  = strtotime($a);
        $stop   = strtotime($b);
        $extend = ($stop-$start)/86400;
        $result['extends'] = $extend;
        if($extend<7){                // If less than 7 Days returns days directly 
            $result['daily'] = $extend;
        }elseif($extend<=31){        // Less than 28 Days return the number of weeks due to leap years 2 Month to satisfy the 
            if($stop==strtotime($a.'+1 month')){
                $result['monthly'] = 1;
            }else{
                $w = floor($extend/7);
                $d = ($stop-strtotime($a.'+'.$w.' week'))/86400;
                $result['weekly']  = $w;
                $result['daily']   = $d;
            }
        }else{
            $y=    floor($extend/365);
            if($y>=1){                // If more than 1 years 
                $start = strtotime($a.'+'.$y.'year');
                $a     = date('Y-m-d',$start);
                // Determine if it's already there 1 Years. If not, subtract 
                if($start>$stop){
                    $a = date('Y-m-d',strtotime($a.'-1 month'));
                    $m =11;
                    $y--;    
                }
                $extend = ($stop-strtotime($a))/86400;
            }
            if(isset($m)){
                $w = floor($extend/7);
                $d = $extend-$w*7;
            }else{
                $m = isset($m)?$m:round($extend/30);
                $stop>=strtotime($a.'+'.$m.'month')?$m:$m--;
                if($stop>=strtotime($a.'+'.$m.'month')){
                    $d=$w=($stop-strtotime($a.'+'.$m.'month'))/86400;
                    $w = floor($w/7);
                    $d = $d-$w*7;
                }
            }
            $result['yearly']  = $y;
            $result['monthly'] = $m;
            $result['weekly']  = $w;
            $result['daily']   = isset($d)?$d:null;
        }
        return array_filter($result);
    }

    print_r(format('2012-10-1','2012-12-15'));
?>

Operation results:
Array([extends]= > 75[monthly]= > 2[weekly]= > 2)

php queries the week of a day and the start date of the corresponding week


/**
* @file
* @version  1.1
* @author  QQ83989686
* @date  2012-8-7  Last modified time 
* @brief 
*/
    // Gets a date   Weeks, weeks corresponding to the start and end time 
    private function getWeekStartEndDay($day)
    {
        $g  = strftime("%u",strtotime($day));
        return array('week_num'=>strftime("%V",strtotime($day)),'week_start_day'=>strftime('%Y-%m-%d',strtotime($day)-($g-1)*86400),'week_start_day_cn'=>strftime('%Y years %m month %d day ',strtotime($day)-($g-1)*86400),'week_end_day'=>strftime('%Y-%m-%d',strtotime($day) + (7-$g)*86400),'week_end_day_cn'=>strftime('%Y years %m month %d day ',strtotime($day) + (7-$g)*86400));
    }


Related articles: