gpt4 book ai didi

php - 如何用PHP生成每月的天数?

转载 作者:行者123 更新时间:2023-12-05 08:19:21 24 4
gpt4 key购买 nike

我试图像这样生成当月的天数

    $year = date('Y');
$month = date('m');

$dayCount = cal_days_in_month(CAL_GREGORIAN,$month,$year);

for ($i = 1; $i <= $dayCount; $i++)
{
$tree_data->data[$i] = $year."-".$month."-".$i;
}
print "<pre>";
print_r($tree_data);

这给了我这样的输出

stdClass Object
(
[data] => Array
(
[1] => 2011-12-1
[2] => 2011-12-2
[3] => 2011-12-3
[4] => 2011-12-4
[5] => 2011-12-5
[6] => 2011-12-6
[7] => 2011-12-7
[8] => 2011-12-8
[9] => 2011-12-9
[10] => 2011-12-10
[11] => 2011-12-11
[12] => 2011-12-12
[13] => 2011-12-13
[14] => 2011-12-14
[15] => 2011-12-15
[16] => 2011-12-16
[17] => 2011-12-17
[18] => 2011-12-18
[19] => 2011-12-19
[20] => 2011-12-20
[21] => 2011-12-21
[22] => 2011-12-22
[23] => 2011-12-23
[24] => 2011-12-24
[25] => 2011-12-25
[26] => 2011-12-26
[27] => 2011-12-27
[28] => 2011-12-28
[29] => 2011-12-29
[30] => 2011-12-30
[31] => 2011-12-31
)

)

我的问题是我想获得 1 - 9 天的时间

2011-12-01, 2011-12-02 etc...

知道如何获得这样的输出吗?

最佳答案

你可以使用 sprintf

sprintf('%1$02d', $i);

另一种方法是使用 DateTime 对象:

$aDates = array();
$oStart = new DateTime('2011-12-01');
$oEnd = clone $oStart;
$oEnd->add(new DateInterval("P1M"));

while($oStart->getTimestamp() < $oEnd->getTimestamp()) {
$aDates[] = $oStart->format('Y-m-d');
$oStart->add(new DateInterval("P1D"));
}

关于php - 如何用PHP生成每月的天数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8616197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com