TRI0N Posted May 14, 2007 Share Posted May 14, 2007 Okay I'm working with MySQL standard date format Y-m-d (2007-5-14). Now what I would like to do is the following with PHP. Extract only this weeks records (7 days): Extract only this months records: (All Records for the month of May 2007): Any help on making this work would be great. I tried searching the forums for "Query Date by Month" and got too much information that didn't even have what I was looking for. Best regards, TRI0N Quote Link to comment https://forums.phpfreaks.com/topic/51368-solved-working-with-dates-y-m-d-mysql/ Share on other sites More sharing options...
Barand Posted May 14, 2007 Share Posted May 14, 2007 <?php /** * this week's data' */ $sunday = date('Y-m-d', strtotime('last sunday')); $saturday = date('Y-m-d', strtotime('next saturday')); $sql = "SELECT * FROM mytable WHERE datecol BETWEEN '$sunday' AND '$saturday' "; // or $sql = "SELECT * FROM mytable WHERE datecol BETWEEN '$sunday' AND '$sunday' + INTERVAL 6 DAY"; /** * this month's data' */ $month = 5; $sql = "SELECT * FROM mytable WHERE MONTH(datecol) = $month"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/51368-solved-working-with-dates-y-m-d-mysql/#findComment-253014 Share on other sites More sharing options...
TRI0N Posted May 14, 2007 Author Share Posted May 14, 2007 Okay lets see.. Now let me use a little bit of my code to make sure this is going to work: $month = date('m') ; $sql = "SELECT * FROM orders WHERE MONTH(d_date) = $month" ; The above orders is the database name and d_date is the row that the Due Date is stored. So if this correct it should work based on what was given above for an answer. Correct me if I'm wrong. The 7 day idea looks like it is only pulling the data out base on that week period rather then next 7 days. This is acceptable but is there a way to pick just the next 7 days? Quote Link to comment https://forums.phpfreaks.com/topic/51368-solved-working-with-dates-y-m-d-mysql/#findComment-253064 Share on other sites More sharing options...
Barand Posted May 14, 2007 Share Posted May 14, 2007 ... WHERE d_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 6 DAY Quote Link to comment https://forums.phpfreaks.com/topic/51368-solved-working-with-dates-y-m-d-mysql/#findComment-253077 Share on other sites More sharing options...
TRI0N Posted May 14, 2007 Author Share Posted May 14, 2007 Thank you so much for you help on this.. All is working Great! Quote Link to comment https://forums.phpfreaks.com/topic/51368-solved-working-with-dates-y-m-d-mysql/#findComment-253085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.