SELECT NOW() - 2 days

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

SELECT NOW() - 2 days

Post by shiznatix »

I am looking for a way to take the NOW() function and subtract 2 days from it but still keep the timestamp in the same format. Basically I have this query:

Code: Select all

SELECT
	bo.id, bo.bonus_code, rm.title
FROM
	rb_bonus_offers AS bo
LEFT JOIN
	rb_rooms AS rm
ON
	bo.fk_room_id = rm.id
WHERE
	bo.active = "1"
AND
	bo.never_expires = "0"
AND
	bo.expiration_date < NOW()
but I want to get WHERE bo.expiration_date < NOW() - 2 days so I can have the data on bonus offers that will expire within 2 days. How do I go about that?
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

Shoot

Code: Select all

SELECT
        bo.id, bo.bonus_code, rm.title
FROM
        rb_bonus_offers AS bo
LEFT JOIN
        rb_rooms AS rm
ON
        bo.fk_room_id = rm.id
WHERE
        bo.active = "1"
AND
        bo.never_expires = "0"
AND
        bo.expiration_date < DATE_SUB(NOW(), 2 DAYS)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Thanks for the kick in the right direction. This was the final trick winner:

Code: Select all

DATE_SUB(NOW(), INTERVAL 2 DAY)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Just a note: you can do the math inline, without the function.

Code: Select all

NOW() - INTERVAL 2 DAY
Post Reply