获取截止到今天为止的月份列表-PHP [英] get a list of months that have passed upto todays date - PHP

查看:333
本文介绍了获取截止到今天为止的月份列表-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个博客档案,我需要列出这些年来的历史月份,到目前为止,例如,今天是8月22日,因此我的档案应显示为

I am building a blog archive, and I need to list out of the historical months for the years, so far example, today is the 22 August so my archive would should show,


  • 八月

  • 七月

  • 六月

  • 五月

  • 四月

  • 三月

  • 二月

  • 一月

  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January

在我的一生中,我无法解决如何输出一年中还没有的所有月份。

I cannot for the life of my work out how to to output all the months of the year that we have not had yet.

推荐答案

您应该查看PHP的

You should look into PHP's DateTime class.

<ul>
<?php
$dateTime = new DateTime();
do {
    $dateTime->modify('+1 month');
    echo '<li>' . $dateTime->format('F') . "</li>\n";
} while ($dateTime->format('m') < 12);
?>
</ul>

您的问题不是很清楚,但根据您的最新评论:

You question wasn't really clear, but according to your latest comment:


但是,此列表显示为12月1月2月2月3月4月5月6月7月,我需要输出该年的所有前几个月,包括当前月份

Thanks, but this list out December January Feburary March April May June July, I need to output all previous months of THIS year including the current month

您想要与我在这里所做的完全相反:

you want the exact opposite of what I have done here:

<ul>
<?php
$dateTime = new DateTime();
echo '<li>' . $dateTime->format('F') . "</li>\n";
$dateTime->modify('-1 month');
while ($dateTime->format('m') != 12) {
    echo '<li>' . $dateTime->format('F') . "</li>\n";
    $dateTime->modify('-1 month');
};
?>
</ul>

这篇关于获取截止到今天为止的月份列表-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆