mysql query by date format

This is my date format in MySQL database

2017-12-22T13:32:48Z

I want to pull all rows from two week prior to today (minus) 7 days.

For example

today is "02/09/2018" I want to get rows from Jan 18 to Feb 1....
LVL 5
CalmSoulAsked:
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.

Dave BaldwinFixer of ProblemsCommented:
In a MySQL Date field, "2017-12-22T13:32:48Z" would have to be "2017-12-22 13:32:48".  The only way to use your format is in a varchar or text field.  Unfortunately, MySQL will only do date comparison arithmetic and logic on Date fields.
0
CalmSoulAuthor Commented:
Thats fine, I can change that, assume I have that what will be the logic ?
0
Dave BaldwinFixer of ProblemsCommented:
I normally use something like this.
SELECT * FROM `table` WHERE `date` > '2018-01-18' AND `date` < '2018-02-02'

Open in new window

Note that I use the 'next' day because it includes all of the previous day because the change occurs at "2018-02-02 00:00:00".
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

CalmSoulAuthor Commented:
Actually, I was thinking if there is an automated way to do this not hard-coded dates, I want to grab this data in re-occurring basis
0
Dave BaldwinFixer of ProblemsCommented:
Certainly.  I use PHP to do this all the time.  I don't know any automated way for MySQL to do it.  There might be but I don't know it.
0
CalmSoulAuthor Commented:
can you share your php code? I will covert it
0
Dave BaldwinFixer of ProblemsCommented:
This is the basic idea.  I use the PHP date functions to create the dates I want and then include them in the SQL string.
<?php 
$stdate = date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")-14, date("Y")));
$endate = date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")-7, date("Y")));
$sql = "SELECT * FROM `table` WHERE `startdate` > '$stdate' AND `enddate` < '$endate'";
$result = $link->query($sql);
// then process the resulting data
 ?>

Open in new window

0

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
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
Databases

From novice to tech pro — start learning today.