adding months to date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

adding months to date

Post by hame22 »

Hi

How can i take todays date and add an extra number of months to it to get a new date. For example create date in 5 months time.

Thanks in advance
bigdavemmu
Forum Newbie
Posts: 22
Joined: Wed Oct 26, 2005 4:01 am

Post by bigdavemmu »

Request the different parts of a date from PHP then add what you need to any part of it.

For example:

$days = date(d);
$month = date(m) + 4;
$year = date(Y);

Then add them together to create a date in the order you wish... For example:

$CompleteDate = $days.'-'.$month.'-'.$year;

This will print 19-01-2007

Hope this helps!

David
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

thanks the proble is it doesnt take into the year, i think there may be an easier way using MYSQL's DATE_ADD
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Take a look at date_modify()
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Grim... wrote:Take a look at date_modify()
8O sweet!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What format is the date in? If your're using a UNIX timestamp, use strtotime(). If you're using a MySQL stamp, use DATE_ADD(). If you're using something else, convert it to a UNIX timestamp.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

If you're stuck with PHP4 you could use mktime() rather than date_modify().
Post Reply