Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Return the year from a future month

    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Maybe something like this:
    PHP Code:
    echo date('Y',strtotime('01/09/2011')); 
    or even something like this:
    PHP Code:
    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 ?

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Agreed, your question makes little sense. Maybe this is what you're trying to accomplish?
    PHP Code:
    $next_year date'Y'strtotime'+1 year' ) ); 
    Edit: 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.
    Last edited by kbluhm; 02-16-2010 at 02:31 PM.

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Function name could use some work
    PHP Code:
    function next_year_by_month$month NULL )
    {
        
    // no month specified... use current, which will always return next year
        
    if ( NULL === $month )
        {
            return 
    date'Y' );
        }
        
    // build a timestamp based on the selected month
        
    $mktime mktime000, ( 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;

    PHP Code:
    // Today is Feb 16, 2010 (2010/02/16)

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

    // January
    echo next_year_by_month); // 2011

    // February
    echo next_year_by_month); // 2011

    // March
    echo next_year_by_month); // 2010 
    Last edited by kbluhm; 02-16-2010 at 02:57 PM.

  • Users who have thanked kbluhm for this post:

    htcilt (02-16-2010)

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks kbluhm,

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

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Gotcha.
    Avoiding some of the newer date features:
    PHP Code:
    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(), 91); 
    No idea if it works, I'm at work so I can't test it. Works alright in my head though

    Edit:
    Bah, way too slow today >.<
    Edit:
    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.

    Last edited by Fou-Lu; 02-16-2010 at 03:09 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    No idea if it works
    Of course it works! Thanks so much everyone


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •