0

我已经生成了一个日期数组。这是一个快速示例

Array
(
[2013] => Array
    (
        [Feb] => 2013
        [Jan] => 2013
    )

[2012] => Array
    (
        [Oct] => 2012
        [Jun] => 2012
        [May] => 2012
    )

)

示例代码:

<?php
$posts = get_posts(array('numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'post', ));
#       $i=1;
print '<pre>';
foreach ($posts as $post) {
    #       $post_results[$post->ID][month] = get_the_time('m', $post->ID);
    #       $post_results[$post->ID][year]  = get_the_time('Y', $post->ID);
    $year = get_the_time('Y', $post -> ID);
    $month = get_the_time('M', $post -> ID);
    $post_results[$year][$month] = get_the_time('Y', $post -> ID);
    #       echo "$i. Post ID: "  .$post->ID." - " .get_the_time('Y-m-d', $post->ID)."<br/>";
    #       $i++;
}

foreach ($post_results as $key => $value) {
    echo "Month: " . $key . " Year: " . $value . "<br/>";
}

print_r($post_results);
print '</pre>';
?>

我想以与此网页相同的格式列出所有日期http://www.smartpassiveincome.com/archives/

我需要的是帮助让我的数组像你在上面看到的那样分隔值。我可以自己做链接部分,我只需要将数据拉入易于使用的变量中。

例如,我想说“对于每一年,列出所有月份,然后输入,<td>$year</td><td>$month</td>然后转到上一年。

4

3 回答 3

1

这将为您提供所需的东西。只需更改$currentDate为您想要开始的任何日期的时间戳即可。

$currentDate = strtotime('January 1st, 2000');
$newestDate = strtotime(date('Y-m-01'));
$yearMonthArray = array();

while ($currentDate <= $newestDate) {
    if (!array_key_exists($year = date('Y', $currentDate), $yearMonthArray)) {
        $yearMonthArray[$year] = array();
    }

    $yearMonthArray[$year][] = date('M', $currentDate);

    $currentDate = strtotime('next month', $currentDate);
}

var_dump($yearMonthArray);

编辑:这只是让你得到所有的年/月组合。您应该能够将此示例与您的代码结合使用,以获得仅显示几个月的帖子。

于 2013-02-13T14:57:33.150 回答
1

这是我认为您需要的,他是实现以下目标的一种方法:

2013 | 二月 | 2012 年1
月 | 十月 | 君 | 可能

原来的:

$arrYears = array(2013=>array("Feb"=>2013, "Jan"=>2013), 2012=>array("Oct"=>2013, "Jun"=>2013, "May"=>2013));
        foreach($arrYears as $strYear=>$arrMonths) {
            echo $strYear;
            // This is our logic for each year
            foreach($arrMonths as $strMonth=>$strYear2) {
                echo " | ".$strMonth;
            }
            echo "<br />";
        }

编辑:将月份放入嵌套列表中

 $arrYears = array(
                 2013=>array("Feb"=>2013, "Jan"=>2013),
                 2012=>array("Oct"=>2013, "Jun"=>2013, "May"=>2013)
              );

 echo "<ul>";
     foreach($arrYears as $strYear=>$arrMonths) {
         echo "<li>";
             echo $strYear;
             echo "<ul>";

                foreach($arrMonths as $strMonth=>$strYear2) {
                    echo "<li><a href='#'>".$strMonth."</a></li>";
                }

             echo "</ul>";
         echo "</li>";
     }
 echo "</ul>";
于 2013-02-13T15:12:55.243 回答
0

我自己想通了,这是我的新代码

$posts = get_posts(
    array(
        'numberposts' => -1,
        'post_status' => 'publish',
        'post_type' => 'post',
    )
);
foreach ($posts as $post) {
    $year = get_the_time('Y', $post->ID);
    $month = get_the_time('M', $post->ID);
    $post_results[$year][$month]  = $month;
}

foreach ($post_results as $key => $value) { 
    echo "<b>"  .$key."</b> ";

    foreach ($value as $sub_key => $sub_value) { 
        echo $sub_key." ";
    }
    echo "<br/>";

}
于 2013-02-13T15:15:38.480 回答