View Full Version : Return the year from a future month
02-16-2010, 02:16 PM
I would like to echo the year for next september e.g. today it would return 2010, on 01/09/2010 it would return 2011.
Does anyone know how I can produce this?
02-16-2010, 02:27 PM
Can you be more specific on what you're looking for? Today is Feb 16, 2010, and you say that this should be year 2010, but that sept 1 2010 should be 2011?
02-16-2010, 02:45 PM
Maybe something like this:
or even something like this:
would be suitable for you ?
I am sorry if my guess on what you need is wrong. In this case as Fou-Lu said could you explain the problem a little bit more in detail please ?
02-16-2010, 03:22 PM
Agreed, your question makes little sense. Maybe this is what you're trying to accomplish?
$next_year = date( 'Y', strtotime( '+1 year' ) );
Oh, I think I get you... as of today, the next September would fall in 2010, but in September 2010, the next September would fall in 2011.
02-16-2010, 03:33 PM
Apologies. I'll try explain a bit better.
I need show to the year for the the next 1st September dynamically, i.e. the function returns the year without any date string entered.
Today (16/02/2010) would return 2010
but from 01/09/2010 the next 1st September would be in the year 2011
on 20/04/2011 the next 1st September would be in the year 2011
2011 would be returned right up until 1st September 2011, when it would then return 2012.
I need something that will run happily without any manual changing of date string.
Let me know if you need any more information or if I'm still not explaining clearly :)
02-16-2010, 03:52 PM
Function name could use some work ;)
function next_year_by_month( $month = NULL )
// no month specified... use current, which will always return next year
if ( NULL === $month )
return 1 + date( 'Y' );
// build a timestamp based on the selected month
$mktime = mktime( 0, 0, 0, ( int ) $month );
// grab the associated year
$year = ( int ) date( 'Y', $mktime );
// if month is current or in the past, reflect the next year
if ( date( 'Y-m' ) >= date( 'Y-m', $mktime ) )
// send it on it's way
// Today is Feb 16, 2010 (2010/02/16)
// Current month
echo next_year_by_month(); // 2011
echo next_year_by_month( 1 ); // 2011
echo next_year_by_month( 2 ); // 2011
echo next_year_by_month( 3 ); // 2010
02-16-2010, 04:01 PM
I would have been here until 1st September trying to figure it out! :thumbsup:
02-16-2010, 04:05 PM
Avoiding some of the newer date features:
function pivotYear($iCurrent, $pMonth, $pDay)
$iYear = (int)date("Y", $iCurrent);
if (!checkdate($pMonth, $pDay, $iYear))
trigger_error('Invalid pivot month/day provided!');
if (date("n", $iCurrent) > $pMonth || (date("n", $iCurrent) == $pMonth && date("j", $iCurrent) >= $pDay))
print pivotYear(time(), 9, 1);
No idea if it works, I'm at work so I can't test it. Works alright in my head though ;)
Bah, way too slow today >.<
OHHHHHHHHH! I guess I didn't getcha lol.
Yes, use kblums, I completely ignored the fact that this should cover past months as well (as in, its not sensitive to the year change). I would expect that mine should return 2010 if provided with a time created in january, instead of 2011 as it should.
02-16-2010, 04:12 PM
No idea if it works
Of course it works! Thanks so much everyone ;)
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.