SkyRanger Posted January 13, 2013 Share Posted January 13, 2013 I am trying to figure out how show a date for the previous month. I already have it figured out to: $todayDate = date("Y-m-d");// current date //Add one month to today $dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month"); echo $dateOneMonthAdded; What I am not sure of is. For example $todayday = 2013-01-31 but I need it to show 2013-02-28 or $todayday = 2013-02-28 and need it to show 2013-03-31 Any help would be greatly appreciated, or will this work the way it is. I am not sure, still new to strotime and dates Quote Link to comment Share on other sites More sharing options...
salathe Posted January 13, 2013 Share Posted January 13, 2013 $todayday = 2013-01-31 but I need it to show 2013-02-28 or $todayday = 2013-02-28 and need it to show 2013-03-31 Could you elaborate a little more on precisely what you want? Is it such that, if the date is the last day of a month then show the last day of the next month? What about the next-to-last day, or the second-to-last day? What would you want to show for the following dates: 2013-02-25 2013-02-26 2013-02-27 2013-02-28 2013-03-01 2013-03-02 The strtotime() function will not do what you're wanting, so you'll need a little bit of code to handle whatever it is that you want to do. Once we can get a better idea of what you're aiming for, some code help shouldn't be too hard to come by. Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted January 13, 2013 Author Share Posted January 13, 2013 Sorry yeah, I should have been a little more clear. for example a client made a payment on 2013-01-31 and there next payment is due at the end of next month.2013-02-28 etc, so if a client makes a payment on the last day of the month I need it to show the due date for the last day of the next month. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 13, 2013 Share Posted January 13, 2013 Are payments always due end of month? Or always 1 month after the last payment? Usually companies do a specific day. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 13, 2013 Share Posted January 13, 2013 So, basically you want to write some code that checks if the due_day exists in the selected month, and if not get the date of what is actually the last day? It's all in how you ask the question, you know. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 13, 2013 Share Posted January 13, 2013 For last day of month echo date ('t M Y', strtotime('2012-02-01')); // 29 Feb 2012 Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted January 13, 2013 Author Share Posted January 13, 2013 For example: client pays: Jan 13 next payment due Feb 13 client pays Jan 31 next payment due Feb 28 Hope this helps Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 13, 2013 Share Posted January 13, 2013 You've been given all the clues you need to solve it. Why don't you try to write the function using that help and if you're still stuck, post back with your code and the problem. Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted January 13, 2013 Author Share Posted January 13, 2013 (edited) Ok, this is what I have so far: $current_date = date('Y-m-d'); $due_date = date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "+1 month" ) ); echo $current_date; echo $due_date; looks like it works so far. Just need a second set of eye's to ensure everything is good and I can finally put this to rest...lol. Plus I just want to be sure that this will also work for end of month that it will update and say: $current_date = 2013-01-31 $due_date = 2013-02-28 Will this code work for what I need? Edited January 13, 2013 by SkyRanger Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 13, 2013 Share Posted January 13, 2013 Why don't you try it out and see? Put it in a loop and go through a bunch of scenarios. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 14, 2013 Share Posted January 14, 2013 I don't think you have really thought this out far enough. Do you realize that the due dates will always be shifted to the 28th or earlier for all payments after February (leap year aside)? So, a user makes a payment on Jan 31st. In Feb, there are only 28 days, so you want the payment to be due on Feb 28th. Then the payment will be due on the 28th of every month thereafter. Or are you wanting the date to go back to the 31st (or the 30th) for months with more days? If so, how are you going to determine the difference between a due date that was originally set on the 28th vs. one that was originally on the 30th or 31st. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 14, 2013 Share Posted January 14, 2013 Solution - store the day of the month that the payment is due Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 14, 2013 Share Posted January 14, 2013 (edited) Also, this line here tells me you really need to step back a bit, and think about what it is your code is actually doing: date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "+1 month" ) ); Let me list the steps taken here: You taking the timestamp for righ now, and making it into a string containing the year, month and day.date("Y-m-d") Then you take that string and convert it into a timestamp.strtotime (...) Which you then take to create a string containing the year, month, and date.date("Y-m-d", .... ) You then concatenate the string "+1 month" to the end of said timestamp, which creates a string like this:1358133991+1 month ... Before you, yet again, try to change it into a timestamp.strtotime (..... ."+1 month") Assuming the above had worked, you are now (yet again) trying to convert the timestamp into a string containing the year, month, and day.date("Y-m-d", ....); The first three steps in this all produce the same timestamp, which means it is all quite wasted effort. Steps 4 & 5 wouldn't work even if you meant to use a comma, as the second parameter is expected to be a timestamp for strtotime (). Which leaves us with only one working and useful line of code, the last. Now, if you look at Barand's reply above you will see the proper way to generate a timestamp for a different date. Also, by reading the PHP manual page for strtotime (), you'd see that it supports quite a few different time formats. Your solution is akin to opening and closing the front door 3 times, before going out the porch door. Which was your intent all along. I hope you agree with me in that doing that doesn't make any sense? Edited January 14, 2013 by Christian F. Quote Link to comment 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.