4

Using bash or mysql , how can I add 30 days in user expiry table?

Example I have DB name USERS and table expiry like this:

USERNAME   EXPIRATION
JOHN       2015-09-26

I want the command to take whatever value is in EXPIRATION column for a specified USERNAME and add 30 days to it

So the result would be:

USERNAME   EXPIRATION
JOHN       2015-10-26
  • 1
    SELECT DATE_ADD(date_field, INTERVAL 30 DAY) AS dd FROM table – taliezin Jul 28 '15 at 11:37
  • @taliezin sorry for providing less information. Now I have updated the Question again. Thank you for time. – Syed Jahanzaib Jul 28 '15 at 11:42
  • 2
    maybe like this select DATE_ADD(expiration, INTERVAL 30 DAY) AS dd from rm_users where username= 'quotauser2'; – Syed Jahanzaib Jul 28 '15 at 11:44
  • Please include the output of describe the-table; – Stéphane Chazelas Jul 28 '15 at 13:15
14

Using GNU date :

$ date -d '2015-09-26 +30 days' '+%Y-%m-%d'
2015-10-26
  • 2
    For '+%Y-%m-%d' short substitution is -I option – Costas Jul 28 '15 at 14:20
1

Here a example for date output as ISO-8601 format one month from now:

date -I -d '+1 month'
  • 1
    One month is not always 30 days. – Kusalananda Apr 5 at 9:24
0

Using MySQL's date_add() function in an UPDATE statement, updating the entry for JOHN. This would update the data in the table.

UPDATE TABLE USERS
SET EXPIRATION = date_add(EXPIRATION, INTERVAL 30 DAY)
WHERE USERNAME = "JOHN"

To just select the the data and increment it for display purposes,

SELECT USERNAME, date_add(EXPIRATION, INTERVAL 30 DAY) AS "EXPIRATION" FROM USERS
WHERE USERNAME = "JOHN"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.