简体   繁体   中英

How can I select rows from a table for specific time ago in php?

I want select all rows from table where added date< today-15 days in php or sql.

Example:

SELECT  * FROM order WHERE date_added< (now()-15days).

How can I do this?

You might want to use date and strtotime function

$fiftendaysago = date('Ymd',strtotime("-15 days"));

And now the query

$sql = "SELECT  * FROM order WHERE date_added< '$fiftendayago' ";

Build the date in php

<?php

$DateString = date('Ymd', strtotime('-15 days'));

$sql = "SELECT  * FROM order WHERE date_added< '$DateString' ";

//Execute your SQL and do your stuff.

i usually use a date_diff function

so the sql query will look like

SELECT  * FROM order WHERE date_diff(now(), date_added) < 15 ;

the function it self

CREATE OR REPLACE FUNCTION date_diff(date, date)
 RETURNS integer AS
 $BODY$
 select $1-$2; $BODY$
LANGUAGE 'sql' VOLATILE
COST 100;

the database will do the calculation. you can pass the interval of the date using a variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM