-1

There is a start date(let it be 16/02/2016) and months(let it be 3 months) given to me. how do I calculate the end date?

Pho Enix
  • 43
  • 10

1 Answers1

0

This will help you

$myDate = "16-02-2016";
$final_date = date('Y-m-d', strtotime("+3 months", strtotime($myDate)));
Aju John
  • 2,214
  • 1
  • 10
  • 27
  • It results into `1970-04-01` – Narendrasingh Sisodia Mar 16 '16 at 07:09
  • But why adding 3 months results in the year 1970? – Pho Enix Mar 16 '16 at 07:12
  • 1
    Its because your date format is in American Format i.e. `mm/dd/yyyy` you need to first convert your date into European date format i.e. `dd-mm-yyyy` so your date must be like as `$myDate = str_replace("/","-","16/02/2016");` and then the above code is ready to go @PhoEnix – Narendrasingh Sisodia Mar 16 '16 at 07:14
  • Uchiha is right.. But It will work if you use m/d/Y format but it will not work with d/m/Y – Aju John Mar 16 '16 at 07:16
  • I'm not getting the result. Also I have to add dynamic months.So is the following code right? $FromDate= str_replace("/","-",$FromDate); $ToDate = date('Y-m-d', strtotime("+".$durationTo, strtotime($FromDate))); – Pho Enix Mar 16 '16 at 07:34
  • The problem with this code if ever you have a date of "2016-01-31" you will get an output of "2016-05-01" – Lee Balino Mar 16 '16 at 07:59
  • So how is it wrong? You want 2016-04-31? – Aju John Mar 16 '16 at 08:03
  • The proper result maybe for after 3 months date will be the last day of the month "2016-04-30" instead getting the 1st day of the next month "2016-05-01" prior on counting by month not the 30 days. If he wanted to produce the per month not skipping a month if the date falls off the range of the days of that month – Lee Balino Mar 16 '16 at 08:12
  • example maybe demonstrated as per month `2016-01-31`, `2016-02-29`, `2016-03-31`, `2016-04-30`. If he wanted it to be like this. Just saying. – Lee Balino Mar 16 '16 at 08:14