peppericious Posted August 16, 2012 Share Posted August 16, 2012 I am inserting NOW() in a datetime field on creation of new records. I want to have a "show this week's entries" link which will display records created on, or since, Monday of the current week. How can I do this? E.g. $q = "SELECT col1, col2 FROM table WHERE creation_date [blah blah]"; $r = mysqli_query($dbc, $q); Thanks in advance for your help. Quote Link to comment https://forums.phpfreaks.com/topic/267163-retrieving-records-created-on-or-after-monday-of-the-current-week/ Share on other sites More sharing options...
peipst9lker Posted August 16, 2012 Share Posted August 16, 2012 You can use mktime() and date() to get the last monday and then convert it into datetime format. Here's a little snippet for datetime conversion, the rest is for you because I don't have much time right now. function strToDateTime($input) { return preg_replace('/^(\d{2}).(\d{2}).(\d{4}) (\d{2})\d{2})\d{2})$/', '$3-$2-$1 $4:$5:$6', $input); } Edit: Forgot to mention, you can use > and < operators in datetime where-clauses. Quote Link to comment https://forums.phpfreaks.com/topic/267163-retrieving-records-created-on-or-after-monday-of-the-current-week/#findComment-1369825 Share on other sites More sharing options...
Barand Posted August 16, 2012 Share Posted August 16, 2012 or SELECT col1, col2 FROM table WHERE WEEK(creation_date,5) = WEEK(CURDATE(),5) Quote Link to comment https://forums.phpfreaks.com/topic/267163-retrieving-records-created-on-or-after-monday-of-the-current-week/#findComment-1369830 Share on other sites More sharing options...
peppericious Posted August 16, 2012 Author Share Posted August 16, 2012 That's just perfect, barand, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/267163-retrieving-records-created-on-or-after-monday-of-the-current-week/#findComment-1369844 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.