简体   繁体   中英

PHP date: How can I check if current year has no more the bygone months?

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?

You could get year right inside the for statement

for($x = $current_month; $x < $current_month+12; $x++) {
    $months_calender[] = date('M', mktime(0, 0, 0, $x, 1));
    $years[] = date('Y', mktime(0, 0, 0, $x, 1));
}
# 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\n"; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM