简体   繁体   English

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

[英]How to get start and end date of months of a given range of date in 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 .预期结果是$startdate$enddate日期范围之间每个月的开始和结束日期数组。

Help me how to do this....帮助我如何做到这一点......

<?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. My Above Function 将给出给定范围内日期的月份范围显示。 This function would be useful for monthly rent calculation as indian tradition.这个 function 对于印度传统的月租计算很有用。

It may help some one else....它可能会帮助其他人....

Have a look at date function and scroll to format character t which gives you the number of days.查看日期 function并滚动到格式字符t ,它会为您提供天数。 Start date will always be 1:-)开始日期将始终为 1:-)

Doing this in a loop for the number of months between the two dates and storing the values in an array is up to you在两个日期之间的月数循环中执行此操作并将值存储在数组中取决于您

function firstAndLast($d=''){
    $d = $d?$d:time();
    $f = mktime(0,0,0,date("n",$d),1,date("Y",$d));
    $l = mktime(0,0,0,date("n",$d),date("t",$d),date("Y",$d));
    return array($f,$l);
}

list($first,$last) = firstAndLast();
echo date("d/m/Y",$first)." ($first) - ".date("d/m/Y",$last)." ($last)";

You can pass a timestamp to the function or leave it blank and it will pick up the current time您可以将时间戳传递给 function 或将其留空,它将获取当前时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM