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条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置