[英]Get last date of the month for the next occuring month
是否可以獲取接下來12個月的月份的最后日期。
例如。 (YYYY-MM-DD)
全年:
today is: 2014-05-09
get 1st month: 2014-06-09
get 2nd month: 2014-07-09
get 3rd month: 2014-08-09
get 4th month: 2014-09-09
get 5th month: 2014-10-09
get 6th month: 2014-11-09
get 7th month: 2014-12-09
get 8th month: 2015-01-09
get 9th month: 2015-02-09
get 10th month: 2015-03-09
get 11th month: 2015-04-09
get 12th month: 2015-05-09
半年:
today is: 2014-05-29
get 1st month: 2014-06-29
get 2nd month: 2014-07-29
get 3rd month: 2014-08-29
get 4th month: 2014-09-29
get 5th month: 2014-10-29
get 6th month: 2014-11-29
get 7th month: 2014-12-29
get 8th month: 2015-01-29
**get 9th month: 2015-02-28**
get 10th month: 2015-03-29
get 11th month: 2015-04-29
get 12th month: 2015-05-29
謝謝
在該月的最后一天,您可以使用:
<?php
$date = time();
$full_year = array();
for($idx = 1 ; $idx <= 12 ; $idx++) {
$tmp = strtotime("last day of next month",$date);
$full_year[] = date("Y-m-d",$tmp);
$date = $tmp;
}
echo '<pre>';
print_r($full_year);
echo '</pre>';
?>
$ full_year變量將包含每個月的日期(從下個月的0開始,以11結尾)(您可以使用任何日期,例如$ full_year [0] ... $ full_year [11])
如果您希望存儲在full_year中的結果與您給出的示例相同(不是該月的最后一天,而是從今天開始的1個月),則可以使用:
<?php
$date = time();
$full_year = array();
for($idx = 1 ; $idx <= 12 ; $idx++) {
$tmp = strtotime("next month",$date);
$full_year[] = date("Y-m-d",$tmp);
$date = $tmp;
}
echo '<pre>';
print_r($full_year);
echo '</pre>';
?>
這是一個想法的示例代碼,它獲取一個月中的總天數,等於該月的最后日期。
$month = 1;
$year = 2014;
echo date('t',mktime(0, 0, 0, $month, 1, $year));
循環的問題是什么? 只要您做對了,您仍然可以在其中分配值。 編輯您的問題,並包括為您分配變量的部分,也許其他所有人都可以提供幫助。
好吧,您可以像這樣手動增加...
$interval="+1 month";
$date=date("Y-m-d");
$month1=date("Y-m-d", strtotime($interval, $date));
$month2=date("Y-m-d", strtotime($interval, $month1));
$month3=date("Y-m-d", strtotime($interval, $month2));
... etc
但是,我敢肯定,您會發現這會變得乏味。
我建議循環,但是將變量存儲在一個數組中(一個可以存儲多個變量的結構...像這樣:
$interval="+1 month";
$months=array();
$date=date("Y-m-d");
array_push($months, date("Y-m-d", strtotime($interval, $date))); //create the first array element, so that we don't have to check it's existence and waste time in the loop
for ($i=1; $i<12; $i++)
{
array_push($months, date("Y-m-d", strtotime($interval, $months[i-1])));
}
我尚未對此進行測試,但無需做太多修改,它應該或多或少可以正常工作:)
不知道為什么需要這樣做而不循環。 Tanatos已就如何循環執行發布了一個很好的答案。 但是因為您說您不想這樣做(無論出於何種原因),所以這里是手動選擇:
$stamp = "last day of next month";
//$stamp = "+1 month"; // Use this if you want what is in your example
$month1 = date('Y-m-d', strtotime($stamp, time()));
$month2 = date('Y-m-d', strtotime($stamp, strtotime($month1)));
$month3 = date('Y-m-d', strtotime($stamp, strtotime($month2)));
$month4 = date('Y-m-d', strtotime($stamp, strtotime($month3)));
$month5 = date('Y-m-d', strtotime($stamp, strtotime($month4)));
$month6 = date('Y-m-d', strtotime($stamp, strtotime($month5)));
$month7 = date('Y-m-d', strtotime($stamp, strtotime($month6)));
$month8 = date('Y-m-d', strtotime($stamp, strtotime($month7)));
$month9 = date('Y-m-d', strtotime($stamp, strtotime($month8)));
$month10 = date('Y-m-d', strtotime($stamp, strtotime($month9)));
$month11 = date('Y-m-d', strtotime($stamp, strtotime($month10)));
$month12 = date('Y-m-d', strtotime($stamp, strtotime($month11)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.