简体   繁体   中英

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.

for example. (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])

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 :

<?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. 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)));

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