Domanda

I have the follow query who allow me to get the expiration date of plan subscription of my site:

SELECT DATE_FORMAT( `expiry_date`, '%d %m %Y' )
FROM `plan_cbsubs_subscriptions` 
WHERE `user_id` = '[user_id]'

So if the plan subscription end the next 27 July, the above query return "27 07 2013". Now my goal is to modify this query for add 3 more day for finally return "30 07 2013" even if the real value is well "27 07 2013".

I suppose is possible to add in the query +3 somewhere, but I don't know the correct syntax.

È stato utile?

Soluzione 3

Something like this should work:

SELECT DATE_FORMAT(DATE_ADD(`expiry_date`,INTERVAL 3 DAY), '%d %m %Y' )
FROM `plan_cbsubs_subscriptions` 
WHERE `user_id` = '[user_id]';

to subtract 3 days:

SELECT DATE_FORMAT(DATE_SUB(`expiry_date`,INTERVAL 3 DAY), '%d %m %Y')
FROM `plan_cbsubs_subscriptions`
WHERE `user_id` = '[user_id]';

you can find more information here: MySQL DateAdd

Altri suggerimenti

SELECT DATE_FORMAT(DATE_ADD(`expiry_date`, INTERVAL 3 DAY) , '%d %m %Y' ) 
FROM `plan_cbsubs_subscriptions` 
WHERE `user_id` = '[user_id]'

Use ADDDATE():

SELECT DATE_FORMAT( ADDDATE(`expiry_date`, 3), '%d %m %Y' ) 
FROM `plan_cbsubs_subscriptions` 
WHERE `user_id` = '[user_id]'

Fyi ADDDATE() will accept negative numbers if you want to subtract days:

ADDDATE(`expiry_date`, -3) -- 3 days before

Mysql also has SUBDATE(), so you can also do:

SUBDATE(`expiry_date`, 3) -- 3 days before
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top