zoukankan      html  css  js  c++  java
  • PHP获取某月天数

    方式一:

    <?php
    
    
    function days($year,$month){
    
        if($month<10){
            $month = '0'.$month;
        }
        if($month == 12){
            $month = '01';
            $year += 1;
        }
    
    
        $curMonth = $year .'-' .$month;
        $nextMonth = $year .'-' .($month+1);
        
        //根据月份,得到第一天
        $start = date("Y-m-d",strtotime($curMonth));
    
        //根据月份,得到下个月的第一天
        $end = date("Y-m-d",strtotime($nextMonth));
    
        $datetime1 = date_create($start);
        $datetime2 = date_create($end);
    
        return intval($datetime1->diff($datetime2)->format('%R%a'));
    
    }
    
    echo days(2015,1)."<br>";
    echo days(2015,2)."<br>";
    echo days(2015,3)."<br>";
    echo days(2015,4)."<br>";
    echo days(2015,5)."<br>";
    echo days(2015,6)."<br>";
    echo days(2015,7)."<br>";
    echo days(2015,8)."<br>";
    echo days(2015,9)."<br>";
    echo days(2015,10)."<br>";
    echo days(2015,11)."<br>";
    echo days(2015,12)."<br>";
    echo days(2016,1)."<br>";
    echo days(2015,12)."<br>";

    方式二:

    <?php
    $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
    echo "There was $num days in August 2003";
    ?>

    方式三:

    //date('m/d/y', strtotime('first day')); # 02/01/10
    //date('m/d/y', strtotime('last day')); # 02/28/10
    //date('m/d/y', strtotime('last day next month')); # 03/31/10
    //date('m/d/y', strtotime('last day last month')); # 01/31/10
    
    date('m/d/y', strtotime('first day')); # 02/01/10
    date('m/d/y', strtotime('first day next month')); # 03/01/10

    方式四:

    $i=12;
    $y=2015;
    echo date("t",strtotime("$y-$i"));
  • 相关阅读:
    搜索进阶1、八数码(HDU1043)
    D.迷宫2 (BFS+优先队列)
    小H的询问(线段树)
    B.迷宫(BFS)
    【UVA】10935 Throwing cards away I(STL队列)
    【UVA】10391 Compound Words(STL map)
    【UVA】12100 Printer Queue(STL队列&优先队列)
    【UVA】1596 Bug Hunt(模拟)
    【UVA】201 Squares(模拟)
    【UVA】1595 Symmetry(模拟)
  • 原文地址:https://www.cnblogs.com/shaoyikai/p/5010595.html
Copyright © 2011-2022 走看看