简体   繁体   English

PHP | 计算日期范围内当前月份的天数

[英]PHP | count num of days for current month in a date range

i am trying to get dates of current month or any other month from a date range. 我正在尝试从日期范围中获取当前月份或任何其他月份的日期。

Let Suppose I have a Date Range as Below. 假设我的日期范围如下。

$startDate = "2014-12-10";
$endDate = "2015-2-3";

I want to count only days/dates of current month "February" which would result in 3 if start date and end date is above one. 我只想计算当前月份"February"天/日期,如果开始日期和结束日期大于1,则结果为3。

but how can i make it work in a programming manner?? 但是如何使它以编程方式工作?

-=-=-=-=-==-= - == - == - == - =

update: 更新:

I think i could not explain my question, 我想我无法解释我的问题,

Lets Take the same date range.. 让我们采用相同的日期范围。

if i want to programmatically take out days of December Month 如果我想以编程方式取出12月的某几天

it would be like 就像

21 days, as start date is 2014-12-10 ; 21天,因为开始日期是2014-12-10

Dates Are in Range Coming Programmatically from database.. 日期在范围内以编程方式来自数据库。

-=-==-=-=-=-=-= - = - = = - == - == - =

UPDATE 2: 更新2:

An other simple example 另一个简单的例子

Lets Suppose If Leaves Have Been Approved For an Employee from 28-1-2015 to 6-2-2015 假设从6-2-20156-2-2015的员工28-1-2015 6-2-2015

so Here Employees Leaves Taken Start Date Would Be 所以这里的员工离职开始日期应该是

$sartDate = '28-1-2015';
$endDate = '6-2-2015';

So Total Leaves Employee would be taking is 因此,员工的总离职时间是

$totalleaves = $endDate - $startDate //It is not right way to take out the differece only For sake of Example shown it

which would give me total leaves 9 or 10 days 这将使我总共离开910

But If We See, These Leaves Are Divided in Two Different Months. 但是,如果我们看到的话,这些叶子被分为两个月。 And i want to generate a Report and i want to see how many leaves employee has taken for specific month which is lets suppose last month January 我想生成一个报告,我想查看员工在特定月份已休了多少假,这是假设上个月的January

it would be 4 days i suppose for below dates as below dates comes in date range and they belong to January. 我想这是4天,因为以下日期属于日期范围,它们属于一月。

28-1-2015
29-1-2015
30-1-2015
31-1-2015

so if i would like to have a result of array of every month leaves it would be like 所以,如果我想得到每个月的数组结果,离开就好了

array(
'January' => array(
 'TotalLeavesTaken' => 4
),
'February' => array(
'TotalLeavesTaken' => 6
)
);

I think thats the best i could explain.. 我认为那是我能解释的最好的..

Adjusted after update in question 有问题的更新后调整

Ok, now I have adjusted after your last update. 好的,现在您上次更新后我已经调整了。 Hope it is what you're looking for: 希望它是您要寻找的:

function getLeavesInPeriod($start, $end) {

    $date = new DateTime($start);
    $endDate = new DateTime($end);

    $leaves = array();

    while($date <= $endDate ) {
        $year = $date->format('Y');
        $month = $date->format('M');        

        if(!array_key_exists($year, $leaves))
            $leaves[$year] = array();
        if(!array_key_exists($month, $leaves[$year]))
            $leaves[$year][$month] = 0;

        $leaves[$year][$month]++;
        $date->modify("+1 day");
    }
    return $leaves;
}

$leaves = getLeavesInPeriod("2015-1-5", "2015-2-3");
print $leaves[2015]["Jan"]; //27

不确定是否正确理解了您的问题,

date('d', strtotime($endDate));

Lets try this 让我们尝试一下

$month = strtotime(date('Y-m-01', time()));
$daysCount = (int)(time() - $month) / (24 * 3600);

It will help you: im using this to get days 它将为您提供帮助:即时通讯可帮助您获得成功

$date_diff = $end_date - $start_date;
//difference of two  dates

This will return you number of days.Is just you want this or something else.Please confirm if it will help you. 这将返回您的天数。仅仅是您想要的东西还是其他。请确认是否有帮助。

$days_left = floor($date_diff / (60*60*24));// No. of days

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

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