显示特定月份的日期范围

时间:2015-08-21 06:38:28

标签: php

我想在php中显示特定月份的日期范围,如下所示:

假设我选择了2015年1月和1月,那么代码应显示:

Date 01/01/2015 to 10/01/2015
     10/01/2015 to 20/01/2015
     21/01/2015 to 31/01/2015

请尽快帮我解决这个问题。 提前谢谢..

2 个答案:

答案 0 :(得分:1)

试试这段代码

          $list=array();
          $month = 11;
          $year = 2014;

      for($d=1; $d<=31; $d++)
      {
         $time=mktime(12, 0, 0, $month, $d, $year);          
        if (date('m', $time)==$month)       
         $list[]=date('Y-m-d-D', $time);
      }
         echo "<pre>";
       print_r($list);

       echo "</br>";
       echo $list[0] . "  to  " . $list[9] ."</br>";

      echo $list[10]. "  to  " .$list[19]  ."</br>";

      echo $list[20]. "  to  " . end($list) ."</br>";

     echo "</pre>";

输出

     2014-11-01-Sat  to  2014-11-10-Mon 
     2014-11-11-Tue  to  2014-11-20-Thu 
     2014-11-21-Fri  to  2014-11-30-Sun

答案 1 :(得分:0)

试试这个

function dateRange( $first, $last, $step = '+10 days', $format = 'Y/m/d' ) {

    $dates = array();
    $current = strtotime( $first );
    $last = strtotime( $last );

    while( $current <= $last ) {

        $dates[] = date( $format, $current );
        $current = strtotime( $step, $current );
    }

    return $dates;
}


$dates = dateRange( '2010/07/26', '2010/08/05') ;
foreach($dates as $date)
{
    echo $date;

}