php - 给定开始日期和持续时间,如何计算每月的天数?

标签 php datetime codeigniter

试图找出一个小问题。我有一个开始日期和一个持续时间(以周为单位),需要计算持续时间内每月的工作天数。

例如: 开始日期:2011-02-07 持续时间:10 周

我想取回以下内容:

二月:16 天 三月:23天 四月:11天

任何帮助都会很棒。谢谢。

最佳答案

5.3 之前的解决方案:

$start = $current = strtotime('2011-02-07');
$end = strtotime('+10 weeks', $start);
$months = array();
while($current < $end) {
    $month = date('M', $current);
    if (!isset($months[$month])) {
        $months[$month] = 0;
    }
    $months[$month]++;
    $current = strtotime('+1 weekday', $current);
}
print_r($months);

输出(codepad):

Array
(
    [Feb] => 16
    [Mar] => 23
    [Apr] => 11
)

关于php - 给定开始日期和持续时间,如何计算每月的天数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4670486/

相关文章:

MYSQL一天间隔从00 :00:00 to 23:59:00

php - 有没有办法根据日期倒计时 4 周(一周一周)? PHP/MySQL

php - 用户不更新 Laravel

php - 意外丢失 session 数据

php - 将数组保存到mysql数据库中

Python xlrd 读取 DateTime 为序列号

php - 根据日期时间列查询记录

php - 禁用 Codeigniter 日志记录并允许原生 PHP 日志记录

php - Codeigniter 函数 "from_label()"给出错误

api - Codeigniter REST API、json 响应中缺少最后几个字符