0

I have mysql date like this 2013-10-16 17:44:13 and I need to get +30 days from there. I can't use eg. 10th ( month ) and change it to 11th ( month ) as this may be 31 day or 29 depending on the month

I can only think of converting 2013-10-16 17:44:13 to timestamp than + 30*24*60*30, and than this new timestamp back to mysql format

Is there a better way?

Kara12
  • 3
  • 1
  • 3
  • 1. There are 60 seconds in a minute, not 30. [`30*24*60*60`] 2. http://php.net/manual/en/datetime.add.php – Sammitch Oct 16 '13 at 18:38

2 Answers2

3

You can use strtotime for this:

$date = date('Y-m-d H:i:s', strtotime($date . ' +30 days'));

or do it directly in MySQL using DATE_ADD:

SELECT DATE_ADD(`date`, INTERVAL 30 DAY) as `date` FROM `table`

If you run a newer version of MySQL, you don't need to use DATE_ADD:

SELECT (`date` + INTERVAL 30 DAY) as `date` FROM `table`

Please note that while strtotime is smart enough, MySQL requires you to use DAY. Not DAYS.

Edit: I am unable to find any proof of DATE_ADD being needed in older versions, but I swear that I've heard it somewhere. Take it with a grain of salt and use whatever method you prefer.

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
2

since you mentioned mysql you an do it with mysql functions

select NOW() + interval 30 day as NEW_DATE

NOW, could be replaced with a date in your db

select date_field + interval 30 day as NEW_DATE from YOUR_DB