繁体   English   中英

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

[英]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.

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