We help IT Professionals succeed at work.

PHP Date Manipulation (Add 1 Year)

socross
socross asked
on
1,884 Views
Last Modified: 2009-12-21
Hi

I am working with two date values 1 in the mysql db format (Y-m-d) and a php formattd version (m-d-Y)

I need to write a funtion that will add 1 year to a date in one of these formats, (infact it would be good to know how to add a year/day/month to any given date in any format.

Many thanks
--s--
Comment
Watch Question

Aleksandar BradarićSoftware Developer
CERTIFIED EXPERT

Commented:
Something like this:
---
<?php
  $date = new DateTime("2007-10-03");
  $date->modify("+1 year");
  echo $date->format("Y-m-d");
?>
---
Aleksandar BradarićSoftware Developer
CERTIFIED EXPERT

Commented:
Or if you don't use objects:
---
<?php
  $date = date('Y-m-d', strtotime('2007-09-01 +1 years'));
  echo $date;
?>
---

Author

Commented:

1.
is there any chance you could write the version without objects into a function i just cant get it to work in a function

function modify_date($date, $string){
     
     //modify date here
   
}

//call function
$newdate = ('09-03-2007', '+1 years');


2.
NB When i tried to use the object version i got call to undefined function. Any thoughts


Many thanks
--s--
Software Developer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Great stuff thanks

-s-
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.