簡體   English   中英

PHP日期:如何檢查當前年份是否已經過去幾個月?

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

我如何檢查當前年份(例如2013年)是否已經過去幾個月(如1月,2月... 10月),然后做點什么?

我有這些代碼行,

# 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));

獲取下面的月份列表,

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 )

然后我想打印月份所屬的年份,

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

有任何想法嗎?

你可以在for語句中獲得年份

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"; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM