jarvis Posted July 27, 2010 Share Posted July 27, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 27, 2010 Share Posted July 27, 2010 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)); Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1091661 Share on other sites More sharing options...
jarvis Posted July 27, 2010 Author Share Posted July 27, 2010 Thanks PFMaBiSmAd but I'm not too sure what you mean :-( sorry Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1091664 Share on other sites More sharing options...
aleX_hill Posted July 27, 2010 Share Posted July 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1091671 Share on other sites More sharing options...
jarvis Posted July 27, 2010 Author Share Posted July 27, 2010 Thanks aleX_hill, Something as smiple as if ($nextMonth > 12) { $year2 = date("o") + 1; echo $year2; } else { $year2 = date("o") ; echo $year2; } I think this will do the job nicely!? Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1091708 Share on other sites More sharing options...
aleX_hill Posted July 27, 2010 Share Posted July 27, 2010 I would still use date("Y") for this, there is no need to use "o". I have never used it, but i can envisage getting some errors in some circumstances. Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1091720 Share on other sites More sharing options...
Psycho Posted July 27, 2010 Share Posted July 27, 2010 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"); Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1091745 Share on other sites More sharing options...
jarvis Posted July 28, 2010 Author Share Posted July 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1091982 Share on other sites More sharing options...
aleX_hill Posted July 28, 2010 Share Posted July 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1092057 Share on other sites More sharing options...
Psycho Posted July 28, 2010 Share Posted July 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209001-php-next-month-issue-with-year-end/#findComment-1092144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.