0

我通过这样的循环构造了一系列日期:

$months = array();
while($j<=12)
{
    $months[] = '2013- .$j . '-01';
    $j++
}

这就像我想要的那样回响。但是,当我使用该date(F, $months[$j])功能时,它每次都会在 1 月出现。这是因为它不是公认的日期格式吗?我该如何解决这个问题?

4

3 回答 3

2

您是否$j在此处复制的内容之外的某个地方增加?否则,这就是你的答案。

$months = array();
$j = 1;
While($j<=12)
{
    $months[] = '2013-' .$j . '-01';
    $j++;
}

另外,您应该传递的不是日期,而是时间戳:

date('F', strtotime($months[$j]))

此外,您可能已经考虑到 $j 从 0 开始,但您将它们存储在$months索引 0 中。所以您可以将其更改为:

$months = array();
$j = 1;
While($j<=12)
{
    $months[$j] = '2013-' .$j . '-01';
    $j++;
}
于 2013-07-26T11:23:24.390 回答
1

使用 www.php.net/strtotime

date('F',strtotime($months[$j]))

于 2013-07-26T11:26:16.557 回答
0

用这个 :

$months = array();
$j=1;
While($j<=12)
{
$months[] = '2013-' .$j . '-01';
$j++;
}

$date = new DateTime($months[2]);
echo $date->format('F');

参考:http ://www.php.net/manual/en/datetime.format.php

于 2013-07-26T11:27:54.657 回答