Jump to content

Today + 1 Month


SkyRanger

Recommended Posts

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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by SkyRanger
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

  1. You taking the timestamp for righ now, and making it into a string containing the year, month and day.
    date("Y-m-d")


  2. Then you take that string and convert it into a timestamp.
    strtotime (...)


  3. Which you then take to create a string containing the year, month, and date.
    date("Y-m-d", .... )


  4. You then concatenate the string "+1 month" to the end of said timestamp, which creates a string like this:
    1358133991+1 month

    ...

  5. Before you, yet again, try to change it into a timestamp.
    strtotime (..... ."+1 month")


  6. 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? :P

Edited by Christian F.
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.