dtrt2368 2011-07-07 14:30
浏览 79
已采纳

如何在php中获取给定日期范围的月份的开始和结束日期

I am using following codes to display start and end dates of current month.

function firstOfMonth() {
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
}

function lastOfMonth() {
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
}

$date_start = firstOfMonth();
$date_end  = lastOfMonth();
echo $date_start;
echo $date_end;

Question: How to get start and end dates of all months in a range of date given

For Eg:

function daterange($startdate,$enddate)
{
...
...
...
}

Expected result be array of start and end dates of each month between date range of $startdate and $enddate.

Help me how to do this....

  • 写回答

3条回答 默认 最新

  • dongxun3424 2011-07-20 14:09
    关注
    <?php
    //Function to return out start and end dates of all months in a date range given
    
    function rent_range($start_date, $end_date)
    {
        $start_date = date("m/d/Y", strtotime($start_date));
        $end_date   = date("m/d/Y", strtotime($end_date));
        $start = strtotime($start_date);
        $end = strtotime($end_date);
    
        $month = $start;
        $months[] = date('Y-m', $start);
        while($month < $end) {
          $month = strtotime("+1 month", $month);
          $months[] = date('Y-m', $month);
        }
    
        foreach($months as $mon)
        {
            $mon_arr = explode( "-", $mon);
            $y = $mon_arr[0];
            $m = $mon_arr[1];
            $start_dates_arr[] = date("m/d/Y", strtotime($m.'/01/'.$y.' 00:00:00'));
            $end_dates_arr[] = date("m/d/Y", strtotime('-1 minute', strtotime('+1 month',strtotime($m.'/01/'.$y.' 00:00:00'))));
        }
    
        //to remove first month in start date and add our start date as first date
        array_shift($start_dates_arr);
        array_pop($start_dates_arr);
        array_unshift($start_dates_arr, $start_date);
    
        //To remove last month in end date and add our end date as last date
        array_pop($end_dates_arr);
        array_pop($end_dates_arr);
        array_push($end_dates_arr, $end_date);
    
        $result['start_dates'] = $start_dates_arr;
        $result['end_dates'] = $end_dates_arr;
        return $result;
    }
    
    $start_date = '2011-07-29';
    $end_date = '2012-03-31';
    $res = rent_range($start_date, $end_date);
    echo "<pre>";
    print_r($res);
    echo "</pre>";
    ?>
    

    My Above Function will give the month range display of dates within a given range. This function would be useful for monthly rent calculation as indian tradition.

    It may help some one else....

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图