Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-16-2010, 01:16 PM   PM User | #1
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
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?
htcilt is offline   Reply With Quote
Old 02-16-2010, 01:27 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-16-2010, 01:45 PM   PM User | #3
SKDevelopment
Regular Coder

 
Join Date: Mar 2006
Posts: 238
Thanks: 3
Thanked 37 Times in 37 Posts
SKDevelopment has a little shameless behaviour in the past
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 ?
__________________
PHP Programmer
SKDevelopment is offline   Reply With Quote
Old 02-16-2010, 02:22 PM   PM User | #4
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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.
__________________
ZCE

Last edited by kbluhm; 02-16-2010 at 02:31 PM..
kbluhm is offline   Reply With Quote
Old 02-16-2010, 02:33 PM   PM User | #5
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
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
htcilt is offline   Reply With Quote
Old 02-16-2010, 02:52 PM   PM User | #6
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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 
__________________
ZCE

Last edited by kbluhm; 02-16-2010 at 02:57 PM..
kbluhm is offline   Reply With Quote
Users who have thanked kbluhm for this post:
htcilt (02-16-2010)
Old 02-16-2010, 03:01 PM   PM User | #7
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
Thanks kbluhm,

I would have been here until 1st September trying to figure it out!
htcilt is offline   Reply With Quote
Old 02-16-2010, 03:05 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 02-16-2010 at 03:09 PM..
Fou-Lu is offline   Reply With Quote
Old 02-16-2010, 03:12 PM   PM User | #9
htcilt
Regular Coder

 
Join Date: Sep 2007
Posts: 238
Thanks: 9
Thanked 0 Times in 0 Posts
htcilt is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
No idea if it works
Of course it works! Thanks so much everyone
htcilt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:17 AM.


Advertisement
Log in to turn off these ads.