views:

222

answers:

2

I have publish up date and publish down date in my DB. Currently they are both same dates.

How do I change it (during mysql insert) so publish down date is 30 days past publish up date.

I am using $pubDate

Thanks

+8  A: 

You can use DATE_ADD():

DATE_ADD(my_date, INTERVAL 30 DAY)
Greg
Typo in your interval, should be:DATE_ADD(my_date, INTERVAL 30 DAY) -- DAY not plural in mysql
Jay
Oops, I always do that - thanks for spotting it.
Greg
Syntactic sugar: instead of date_add() you can also use `my_date+Interval 30 DAY` and instead of date_sub() `my_date-Interval 30 DAY`.
VolkerK
in my php I have mysql insert.....VALUES ('$pubDate', 'DATE_ADD($pubDate, INTERVAL 30 DAY)')....$pubDate is datetime type btw. Currently it gives me 0000-00-00 00:00:00 whats wrong?
Ossi
You have quotes around the wrong bit: VALUES ('$pubDate', DATE_ADD('$pubDate', INTERVAL 30 DAY))
Greg
Works great, ie it adds correct date, altough Mysql gives me Notice: a non well formed numeric value encountered. Do I need to worry about this? can I "suppress" the notice's. I'll accept this answer now ;)
Ossi
+1  A: 

in php, before inserting you can use strtotime(): if the publishDown date is a timestamp:

$publishDown = strtotime("+30 days",$publishDown);

otherwise you may have to use mktime to get it in the right format

GSto
If you were reading a date from the database and displaying it, I'd say manipulate the date with your server-side language. When you're storing data, use the database's internal functions for any data manipulation.
iKnowKungFoo
I gave you an upvote because I think this is a perfectly reasonable solution to the problem, in fact, it may be better than using the database's internal functions as iKnowKungFoo suggested because every darn database has different internal functions for doing the same darn thing.
John Scipione