adding a week to a mysql retrieved 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
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

adding a week to a mysql retrieved date

Post by andym01480 »

Got a database that handles a weekly rota, so each row contains a date in the form yyyy-mm-dd.

I want to add another years worth of dates automatically.

I can grab the last date in the the table

Code: Select all

$query="SELECT date from sundayrota order by date desc";
$result=mysql_query($query);
$lastdate=mysql_fetch_assoc($result);
So $lastdate[date] contains 2006-12-31

What do I do to make it in a form that I can iterate adding 7 days to it 52 times and inserting it back in!

Can you help please
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If you stick with mysql you can let it calc the date

Code: Select all

SELECT
	date, date + Interval 1 year as future
FROM
	sundayrota
ORDER BY
	date desc
if you want php to do it you can use strtotime

Code: Select all

<?php
$future = strtotime('2006-12-31 + 1 year');
echo date('Y-m-d', $future);
?>
see http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html and http://de2.php.net/strtotime
Post Reply