简体   繁体   English

获取下一个月的月份的最后日期

[英]Get last date of the month for the next occuring month

Is it possible to get the last date of the month for the next 12 months. 是否可以获取接下来12个月的月份的最后日期。

for example. 例如。 (YYYY-MM-DD) (YYYY-MM-DD)

full year: 全年:

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

half year: 半年:

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

thank you 谢谢

For the last day of the month you can use : 在该月的最后一天,您可以使用:

<?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 variable will contain the date for each month (starting from 0 for next month and ending with 11) (you can use any of the dates like $full_year[0]...$full_year[11]) $ full_year变量将包含每个月的日期(从下个月的0开始,以11结尾)(您可以使用任何日期,例如$ full_year [0] ... $ full_year [11])

If you want the result stored in full_year to be the same as the example you gave (which is not the last day of the month, but simply 1 month from today), you can use : 如果您希望存储在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>';
?>

here's a sample code of the idea, this gets the total number of days in a month, which, equals to the last date of the month. 这是一个想法的示例代码,它获取一个月中的总天数,等于该月的最后日期。

$month = 1;
$year = 2014;
echo date('t',mktime(0, 0, 0, $month, 1, $year));

and what's the problem of looping? 循环的问题是什么? you could still assign values in it as long as you do it right. 只要您做对了,您仍然可以在其中分配值。 edit your question and include the part you assign variables and maybe everyone else can help. 编辑您的问题,并包括为您分配变量的部分,也许其他所有人都可以提供帮助。

Well you can increment manually, like this... 好吧,您可以像这样手动增加...

$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

But I am sure you can see how that could get tedious. 但是,我敢肯定,您会发现这会变得乏味。

I would recommend looping, but storing your variables in an array (a construct with which you can store more than one variable... Like so: 我建议循环,但是将变量存储在一个数组中(一个可以存储多个变量的结构...像这样:

$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])));
}

I haven't tested this yet but it should work more or less fine without much modification:) 我尚未对此进行测试,但无需做太多修改,它应该或多或少可以正常工作:)

Not sure why you need to do this without looping. 不知道为什么需要这样做而不循环。 Tanatos has posted a great answer on how to do this with a loop. Tanatos已就如何循环执行发布了一个很好的答案。 But because you said you didn't want to do it this way (for whatever reason), here is the manual alternative: 但是因为您说您不想这样做(无论出于何种原因),所以这里是手动选择:

$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.

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