Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL SELECT records older than 1 year ago

Tags:

date

php

mysql

I try to get a list of all records that are older than 1year ago from my database, the field for expired_contract has next information.

expired_contract DATE NOT NULL 

So it takes the DATE in the next format: YEAR-MM-DD, next i have the sql that i cant get it working sadly.

$sql = "SELECT *          FROM My_Contracte          WHERE expired_contract >= DATE_SUB(NOW(),INTERVAL 1 YEAR)          ORDER BY id_contract DESC"; 

I tried a lot of "WHERE" commands but none worked as i expected. Can you help me get this working? I'm looking on this for about 5hours i need exact command to get it worked.

The $sql gets me something but takes it wrong, i get dates like: 2015-10-01, 2016-10-01 and date like 2014-09-30 doesn't show up.

Basically i want to show dates like:

If today is 2015-10-01 i want to see dates older than 1year ago so from 2014-09-30 and not showing dates like 2015-10-01, 2016-10-01.

Maybe do i have to edit something in database?

Looking for your help, thank you!

like image 937
Alex Grecu Avatar asked Oct 01 '15 11:10

Alex Grecu


1 Answers

You have to use lower than instead of greater or equals:

$sql = "SELECT * FROM My_Contracte WHERE expired_contract < DATE_SUB(NOW(),INTERVAL 1 YEAR)  
like image 162
Jens Avatar answered Oct 19 '22 00:10

Jens