dougui4325 2013-11-06 13:32
浏览 29
已采纳

PHP日期:如何检查当前年份是否已经过去几个月?

How can I check if current year (2013 for instance) has no more the bygone months (like Jan, Feb...Oct), then do something?

I have these lines of code,

# Set month array for the calendar.
$months_calender = array();

# Set current month and curren year.
$current_month = (int)date('m');
$current_year = (int)date('Y');

for($x = $current_month; $x < $current_month+12; $x++) $months_calender[] = date('M', mktime(0, 0, 0, $x, 1));

to get the month list below,

Array (
    [0] => Nov
    [1] => Dec
    [2] => Jan
    [3] => Feb
    [4] => Mar
    [5] => Apr
    [6] => May
    [7] => Jun
    [8] => Jul
    [9] => Aug
    [10] => Sep
    [11] => Oct )

Then I want to print the year that the month belongs to,

foreach($months_calender as $index => $month_calender)
{
    if current year has no more Jan then print next year, for instance 2014
}

Any ideas?

  • 写回答

2条回答 默认 最新

  • dougaimian1143 2013-11-06 13:43
    关注
    # Set month array for the calendar.
    $months_calender = array();
    $current_month = (int)date('m');
    
    for($x = $current_month; $x < $current_month+12; $x++) {
        $time = mktime(0, 0, 0, $x, 1);
        $months_calender[] = array(date('M', $time), date('Y', $time));
    }
    
    foreach($months_calender as $monthYear) {
       list($month, $year) = $monthYear;
       echo "$month, $year
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗