PHP Date Manipulation (Add 1 Year)

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--
LVL 1
socrossAsked:
Who is Participating?
 
Aleksandar BradarićConnect With a Mentor Software DeveloperCommented:
Just extract the date part out of the string like:
---
  $in_date = '2007-09-01';
  $out_date = date('Y-m-d', strtotime($in_date . ' +1 years'));
  echo $out_date;
---
0
 
Aleksandar BradarićSoftware DeveloperCommented:
Something like this:
---
<?php
  $date = new DateTime("2007-10-03");
  $date->modify("+1 year");
  echo $date->format("Y-m-d");
?>
---
0
 
Aleksandar BradarićSoftware DeveloperCommented:
Or if you don't use objects:
---
<?php
  $date = date('Y-m-d', strtotime('2007-09-01 +1 years'));
  echo $date;
?>
---
0
 
socrossAuthor 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--
0
 
socrossAuthor Commented:
Great stuff thanks

-s-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.