如何获取列出两个日期之间的所有月份数据PHP

时间:2018-08-10 12:22:26

标签: php

如何使用PHP查找两个日期之间的月数? 并提供如下所示的日期

# making Data Frame
np.random.seed(41)
age=np.random.randint(1,50,5,int)
name=['kashif', 'dawood', 'ali', 'zain', 'hamza']
df=pd.DataFrame({'name':name, 'age':age})

# create a bin
bins = [0, 18, 50]
# create a bin label
label_list = ['adult', 'old']
# create a new column with bin and label
df['status'] = pd.cut(df.age, bins, labels=label_list)

我想要每月输出类似

$fromdate = 12-01-2018
$todate = 18-04-2018

我尝试过

12-01-2018 - 12-02-2018
12-02-2018 - 12-03-2018
12-03-2018 - 12-04-2018
12-04-2018 - 18-04-2018

1 个答案:

答案 0 :(得分:1)

您可以使用此代码。它将$start(= $fromdate)增加一个月,直到等于或晚于$end(= $todate)。

$fromdate = '12-01-2018';
$todate = '18-04-2018';

$start = DateTime::createFromFormat('d-m-Y', $fromdate);
$end = DateTime::createFromFormat('d-m-Y', $todate);

do {
    echo $start->format('d-m-Y') . ' - ';
    $start->add(new DateInterval('P1M'));
    if ($start >= $end) {
        echo $end->format('d-m-Y') . "\n";
        break;
    }
    echo $start->format('d-m-Y') . "\n";
} while (true)

输出:

12-01-2018 - 12-02-2018
12-02-2018 - 12-03-2018
12-03-2018 - 12-04-2018
12-04-2018 - 18-04-2018