Advancing a 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
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Advancing a date()

Post by larrytech »

Hi,
I have had a look around for an answer to this, and maybe I am searching for the wrong thing because I cannot find anything!

I have a date in a mysql database that is in the format 2003-01-29. This is then formatted on the actual page to 29 Jan 2003. I am wondering whether it is possible to easily add thirty days to any date, so that a date in the future is shown exactly thirty days ahead. I expect it should know about the number of days in the month and compensate for this.

Is there an easy was of doing this or would I be better to work it out each time myself?

Thanks a lot,
Lawrence
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might query it from mysql

Code: Select all

SELECT Now() + interval 30 day

Code: Select all

SELECT '2003-01-29' + interval 30 day

Code: Select all

SELECT <date field> + interval 30 day as future FROM <tablename>
or use mktime

Code: Select all

<?php
$date = getdate();
$future = mktime (0,0,1, $date['mon'], $date['mday']+30, $date['year']);
echo date("F j, Y, g:i a", $future);
?>

Code: Select all

<?php
$date = getdate(strtotime ('29 Jan 2003'));
$future = mktime (0,0,1, $date['mon'], $date['mday']+30, $date['year']);
echo date("F j, Y, g:i a", $future);
?>
larrytech
Forum Commoner
Posts: 35
Joined: Thu Jun 06, 2002 8:27 am

Post by larrytech »

Thank you very much! That works just as wanted.
Lawrence
Post Reply