David Hope
David Hope

Reputation: 1546

PHP: Select from mysql where 1 week's left to datetime?

I have a datetime field in MYSQL database.

They look like this:

2017-02-28 00:15:07

I need to select and return all the results from this field that has 1 week let to that date using PHP...

Is there any built in PHP feature to do this?

something like:

SELECT * FROM tableName WHERE 1 week left to (column name)

any help would be appreciated.

Upvotes: 2

Views: 288

Answers (2)

Ray Hunter
Ray Hunter

Reputation: 15557

You can use interval like the following:

SELECT *  FROM tableName 
WHERE NOW() BETWEEN (column_date - INTERVAL 1 WEEK) AND column_date;

That will let you know if now is between the column date - 1 week and the column date.

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133380

Could be using date_sub and date_create_from_date_string

this is for php

date_sub($date,date_interval_create_from_date_string("7 days"));

http://php.net/manual/en/dateinterval.construct.php

for a select query

select *  FROM tableName 
WHERE your_column_date BETWEEN DATE_SUB(NOW(), INTERVAL 1 week) AND NOW();

Upvotes: 0

Related Questions