Jump to content

php next month issue with year end


jarvis

Recommended Posts

Thanks to aleX_hill and RussellReal for the help so far. I'm trying to utilise the code before to find the next month. However, it needs to be used with the current year. So I altered the below

 

<?php	
$nextMonth = date("m") + 1;	
$daysInNextMonth = date("t",mktime(0,0,0,$nextMonth));	
echo $daysInNextMonth;?>

 

to

#NEXT MONTH
$nextMonth = date("m") + 1;	
echo $nextMonth; 
$daysInNextMonth = date("t",mktime(0,0,0,$nextMonth));	
echo $daysInNextMonth ;
$year = date("o"); 
echo $year;

 

I can then pass it into my url query like so:

<a href="<?php echo $url_path; ?>after=<?php echo $year; ?>-<?php echo $nextMonth;  ?>-01&before=<?php echo $year; ?>-<?php echo $nextMonth;  ?>-<?php echo $daysInNextMonth; ?>">Next Month</a>

 

Brilliant I thought, until it comes to December 2010, the next month needs to be January 2011. Now I'm bamboozled. How do I get around this one?

 

Many thanks, your help is always appreciated!

 

Link to comment
Share on other sites

You need to do two things -

 

1) Put in the 5th parameter in the mktime() function, the day, as the first day of the month (or at least a day that exists in every month), because it is currently using the current day of the month and when you execute that code in a month where the current day does not exist in the next month it will skip over the next month and give you the 2nd following month.

 

2) Do the date math inside the mktime() function so that it will automatically rollover the date correctly -

 

$daysInNextMonth = date("t",mktime(0,0,0,date("m") + 1,1));

 

 

Link to comment
Share on other sites

mktime essentially works like this:

mktime(hour, minute, second, month, day, year);

There is another parameter for daylight savings, but I personally dont need it, you may. The output of the function is the Unix timestamp for that specific second (Unix timestamp is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)). Try a few "echo mktime()" functions to see the different figures.

 

So when you use the date() function, you want need to set the date using mktime in the second parameter. If you dont provide a second parameter, like date("m"); then it assumes you mean the current server time. If you want to use date() on a future time, supply the unix timestamp as the second parameter (hence the use of mktime).

 

Anyway, I would personally use date("Y") to get the current year (unless there is a specific reason you are using "o").

 

So what is happening in your code is this: You get December being month 12, add 1 and you end up with 13 (duh). So the $daysInNextMonth works fine (the 13 means the 1st month of the next year), but when you do the date("o") you are getting the year we are currently in (2010 last time I checked).

 

I would propose checking if $nextMonth is > 12, then add 1 to $year (for the next date, but make sure you dont add 1 for the "before" part

Link to comment
Share on other sites

You are making this way harder than it needs to be. Can you give some insight as to the source and purpose of the variables $nextMonth and $year2 and how they are modified?

 

If "next month" is literally supposed to be next month, then you could simply use strtotime()

 

$year2 = date('o', strtotime("next month");

Link to comment
Share on other sites

Thanks mjdamato and again to aleX_hill. The reason I'm using this code is to locate the next month. However, if you're in December, the next month is January, so I also need to take the year into consideration.

 

This all stems from passing date criteria into a query string, to allow people to search information by this week, next week and next month etc

 

Hope this helps explain why I'm using this code. Thanks again

Link to comment
Share on other sites

Wow, I have never looked at strtotime() before (I dont do a lot of work with time/date). I think I just found a new favourite function. "next Monday" etc would be good for calendars and such. Thanks for pointing it out to me.

Exactly!

 

The reason I'm using this code is to locate the next month. However, if you're in December, the next month is January, so I also need to take the year into consideration.

That is the precise reason to use strtotime()! It will do the work for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.