...

View Full Version : Return the year from a future month



htcilt
02-16-2010, 01: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?

Fou-Lu
02-16-2010, 01: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?

SKDevelopment
02-16-2010, 01:45 PM
Maybe something like this:


echo date('Y',strtotime('01/09/2011'));

or even something like this:


echo substr('01/09/2011',strrpos('01/09/2011','/')+1);

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 ?

kbluhm
02-16-2010, 02: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.

htcilt
02-16-2010, 02: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.

For example

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 :)

kbluhm
02-16-2010, 02: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 ) )
{
$year++;
}
// send it on it's way
return $year;
}



// Today is Feb 16, 2010 (2010/02/16)

// Current month
echo next_year_by_month(); // 2011

// January
echo next_year_by_month( 1 ); // 2011

// February
echo next_year_by_month( 2 ); // 2011

// March
echo next_year_by_month( 3 ); // 2010

htcilt
02-16-2010, 03:01 PM
Thanks kbluhm,

I would have been here until 1st September trying to figure it out! :thumbsup:

Fou-Lu
02-16-2010, 03:05 PM
Gotcha.
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))
{
++$iYear;
}
return $iYear;
}

// Used:
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.

htcilt
02-16-2010, 03:12 PM
No idea if it works

Of course it works! Thanks so much everyone ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum