Date format in php mysql

I have a table in mysql where date1 is datetime type field.
When I query like this
select * from deals where (DATE_FORMAT(date1, '%m/%d/%Y') between '12/01/2017' and '12/31/2017')
but the output shows records with dates of year 2016 also along with 2017 .
Please guide me.
LVL 1
Rajesh JoshiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Using DATE_FORMAT returns a string so you can't do a proper comparison with dates.

If you want to return the deals from 2017 try this.

select * from
deals where
DATE_FORMAT(date1,'%Y') ='2017'
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you should try format in %Y%m%d instead:

select * from deals where (DATE_FORMAT(date1, '%Y%m%d') between '20171201' and '20171231')

Open in new window


or use below to exclude date part:

select * from deals where DATE_FORMAT(date1, '%Y%m') = '201712'

Open in new window


or use Month and Year functions:

select * from deals where year(date1) = 2017 and month(date1) = 12

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
Oops, misread the criteria and thought it was the year 2017 only, not month 12 in year 2017.
Pawan KumarDatabase ExpertCommented:
Do not format the date column in the where clause. That will decrease the performance.

Always use >= and < approach.

Please use like below-

select * from deals
where date1 >= '2017-12-01'  AND date1 < '2018-01-01'

Adjust your input if required.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.