Jump to content

Future date calculation


krishnik007

Recommended Posts

Hi,

 

I want to calculate the future month, i have used following code to calculate the date

$date = date('Y-m-d',strtotime(date("Y-m-d", strtotime('2010-01-31')) . " +3 month"));

 

It gives me 2010-03-03

 

But i need to get 2010-02-28

 

OR just month is enough like 2010-02

 

Thank you

Nikhil

Link to comment
Share on other sites

You want to get the year and month in 3 months time?

 

$month = date('Y-m', strtotime('+ 3 months'));

 

Yes that right adam. I just want to find out the year and month in 3 months time.

$month = date('Y-m', strtotime('+ 3 months'));

 

How can i pass a date to above function suppose i want to find out the value after 3 month of the following date 

2010-01-31

How can i do that..

 

Sorry for my poor english

 

Nick

Link to comment
Share on other sites

If you're trying to get it from a specific date such as in your example (2010-01-31), and not 3 months from the current date, you can use mktime.

 

<?php
$from = '2010-10-01';
list($year, $month, $day) = explode('-', $from);

$date = date('Y-m-d', mktime(0, 0, 0, $month + 3, $day, $year));
echo $date;
?>

 

Link to comment
Share on other sites

Or..

 

$month = date('Y-m', strtotime('+ 3 months', strtotime('2010-01-31')));

 

Of course I just realised in your first example, the date returned is correct. There's no 31st of February so it's carried on to the next month; unfortunately this is the behaviour of strtottime().

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.