23

I have a db table with a date field startBefore in ('Y-m-d') format. There are also 3 other int fields representing years, months and days. What I would like to do is to fill these three fields the new startAfter date in 'Y-m-d' format.

For example I have the '2001-11-14' as startBefore date and I would like to subtract 3 yrs, 7 months, 1 day to get the startAfter date.

Any ideas how I can accomplish this?

Thanks!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
dimoss
  • 479
  • 1
  • 3
  • 10
  • possible duplicate of [Subtracting a certain number of hours, days, months or years from date](http://stackoverflow.com/questions/2382458/subtracting-a-certain-number-of-hours-days-months-or-years-from-date) –  Jan 03 '14 at 10:52

4 Answers4

33

use strtotime('date -years -months -days')

  <?php
    $time = strtotime('2001-11-14 -3 years -7 months -5 days');
    echo $date = date("Y-m-d", $time);
Nambi
  • 11,944
  • 3
  • 37
  • 49
  • 1
    It doesn't work as the result is 1999-06-19 which is wrong. The correct for your example should be 1998-04-13. – dimoss Jan 03 '14 at 11:03
  • it works fine for me it returns 1995-11-14 i'm subtracting 3 years 7 months and 5 days from it. check again and tell me – Nambi Jan 03 '14 at 11:06
  • 1
    Yes, works ok. The result is: 1998-04-09. Starrting to subtract 3 years, 7 months, 5 days from '2001-11-14'. However I don't know if it takes into account the leap years and Feb. – dimoss Jan 03 '14 at 11:21
  • I think the best way to do this is firstly convert years to days, months to days and adding the remaining days use your code to take the result. Thanks! – dimoss Jan 03 '14 at 11:32
  • tomexsans has a better solution IMO, it is more OO, easier to read, less code, and more extensible. – ShaneMit Jul 27 '17 at 19:53
  • For weekdays just use `-weekdays` instead of `-days` – Jason Ellis Aug 01 '18 at 19:50
31

Here comes the DateTime:

$start_date = '2013-03-06';
$date = DateTime::createFromFormat('Y-m-d',$start_date);

$date->modify('+1 month');
echo $date->format('Y-m-d');//2013-04-06

$date->modify('+4 year');
echo $date->format('Y-m-d');//2017-04-06

$date->modify('+6 day');
echo $date->format('Y-m-d');//2017-04-12

$date->modify('+24 hours');
echo $date->format('Y-m-d');//2017-04-13

$date->modify('-7 years');
echo $date->format('Y-m-d'); //2010-04-13

$date->modify('-18 months');
echo $date->format('Y-m-d'); //2008-10-13

So on and so forth.

tomexsans
  • 4,454
  • 4
  • 33
  • 49
2

You Could try

strtotime function in php its quite simple

<?php 
echo date("jS F, Y", strtotime("11.12.10")); 
// outputs 10th December, 2011 

echo date("jS F, Y", strtotime("11/12/10")); 
// outputs 12th November, 2010 

echo date("jS F, Y", strtotime("11-12-10")); 
// outputs 11th December, 2010  
?> 
0

One way is to make the startBefore date a timestamp, than working with numbers and re-obtain the startAfter date, another way is to try to subtract days, months and years directly from the date.

Goikiu
  • 574
  • 3
  • 12
  • How is possible to subtract days, months, years directly from the date? Thanks. – dimoss Jan 03 '14 at 10:50
  • Sorry, i where searching, it seem the you after all need to use timestamp, the "format" and subtract need the timestamp. – Goikiu Jan 03 '14 at 10:56