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
https://forums.phpfreaks.com/topic/210952-future-date-calculation/
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

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;
?>

 

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().

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.