• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1835
  • Last Modified:

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--
0
socross
Asked:
socross
  • 3
  • 2
1 Solution
 
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
 
Aleksandar Bradarić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
 
socrossAuthor Commented:
Great stuff thanks

-s-
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now