jakebur01 Posted October 6, 2011 Share Posted October 6, 2011 I need to do some reporting and I need some help. The reporting periods are as follows: 08/28-09/03 09/04-09/10 09/11-09/17 09/18-09/24 09/25-10/01 So, they are Sunday through Saturday throughout each month. Say the date is Sept. 7.... How can I find out the date of the Sunday before last. How can I know what month I am reporting for? I am querying the database each Wed. for sales data from the previous month. I want to take the totals from my query and store them in a separate table. The problem is, I don't know how to find out what month the previous week falls into. And I don't know how to find the date of the Sunday before last. Thanks, Jake Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/ Share on other sites More sharing options...
requinix Posted October 6, 2011 Share Posted October 6, 2011 To get the previous Sunday (which might be today), function lastsunday($date = null) { if ($date === null) $date = time(); if (!ctype_digit($date)) $date = strtotime($date); return strtotime("-" . date("w", $date) . " days", $date); } Tips: 1. If you want the Sunday between 1 and 8 days ago (ie, if today is Sunday then use the week before's Sunday), give $date=yesterday. 2. Use strtotime to subtract 7 more days if you want the Sunday before. In general, if you need to know something about a date then use date, and if you want to travel forward or backward in time from a date then use strtotime(). Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276570 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 Sample Structure: Id (int 11), Sunday (date), Saturday (date) Sample Data: 0 2011-08-28 2011-09-03 1 2011-09-04 2011-09-10 Let say as an example the table above is filled with every week this year. How could I query it for any month of the year. If I wanted to query it for April 2011, how could I do that and it contain all the weeks that fall within that month? Every Sunday - Saturday period within any month I pick. I can find the previous Sunday, that as not a big of a concern as being able to do this. ___________________________ If it would be hard to query the sample table. I could store the data a different way maybe? Calculate what month the previous week is in before I store it and use a table like this: Id, Month, Year, Sunday, Saturday 0 04 2011 2011-08-28 2011-09-03 Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276810 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 You have Start date (Sunday) and you have an End date (Saturday). What is the problem exactly? If the values in your table are stored as Dates then I don't know why you're having that much trouble retrieving the relevant data. Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276858 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 Let say as an example the table above is filled with every week this year. How could I query it for any month of the year. If I wanted to query it for April 2011, how could I do that and it contain all the weeks that fall within that month? Every Sunday - Saturday period within any month I pick. The problem is I don't know how to retrieve the info above. I have the dates, but I don't know how to retrieve it. It is not going to have a html form field to select date periods. I want it to automatically select the previous months weeks. So, like today if I run the script I want it to know to retrieve the previous month which is September and know what dates to select in september.... which would be 08/28 - 09/03. Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276911 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 Here's some code which produces an array with the 2 dates... Was that what you requested? Your question was vary vague. $dates = array( date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday', strtotime('last month')))), date('Y-m-d', strtotime('last Saturday', strtotime('last month'))) ); The only problem is if you're checking this on a 29th, 30th or 31 and in some months it will skip a month (-30 days) dunno how you solve that. For example, March 31 - 30 days is 1 March. Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276921 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 Let me be more specific. Look at the table. table Id Saturday Sunday 0 2011-07-31 2011-08-06 1 2011-08-07 2011-08-13 2 2011-08-14 2011-08-20 3 2011-08-21 2011-08-27 4 2011-08-28 2011-09-03 5 2011-09-04 2011-09-10 6 2011-09-11 2011-09-17 7 2011-09-18 2011-09-24 8 2011-09-25 2011-10-01 9 2011-10-02 2011-10-08 If the current date is 2011-10-07, how could I use php to query the table for last months periods (which are Sat. 8/28 - Sun. 10/01)? How can I get php to know how my week periods are setup? I could set up a separate table and store the period range for each month, but I would rather have the whole thing do it automatically if I could. Take a look at how our periods are set up below. It will help if you look on a calendar to see what I am talking about. Jan. 1/2-1/29 Feb. 1/30-2/26 Mar. 2/27-3/26 Apr. 3/27-4/30 May 5/1-5/28 Jun. 5/29-6/25 July 6/26-7/30 Aug. 7/31-8/27 Sept. 8/28-10/1 Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276987 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 Have you even run my code? Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276993 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 Ok... This may help. I just learned this. Our company uses 4-week and 5-week periods. The last friday of the month makes up the last week of the month. No, I have not run it yet. I wasn't sure if you understood what I was trying to do or not. Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276994 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 If I could find the last Friday of the previous month and add one.... this would give me my ending period date. And I found the last Friday for the month before last and add two... this would give me my Starting period date. Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1276996 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 I ran your code and got these two dates: 2011-08-28 2011-09-03 Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277000 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 First you asked how to find out a week period starting from Sunday and ending the following Saturday in the last month and now you ask for something else. Yeah my code generated those two dates, wasn't it those you asked for? Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277005 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 Sorry... Yes, thank you. I guess I got so caught up in trying to find out the last friday of the previous month that I forgot what my original question was. Should that code find the previous Sunday - Saturday no matter what week you are in? Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277007 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 That is correct. It takes today's date. Then subtracts 30 days. Then returns the previous Saturday from this date. The second date is the previous Sunday from this new date. Want to find out the last friday of the previous month? echo date('Y-m-d', strtotime('last Friday', strtotime('first day of this month'))); Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277014 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 Here's the code to generate a period. $dates = array( date('Y-m-d', strtotime('+2 days', strtotime('last Friday', strtotime('first day of this month last month')))), date('Y-m-d', strtotime('+1 day', strtotime('last Friday', strtotime('first day of this month')))), ); print_r($dates); Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277022 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 And here's the code to generate that table of yours, for testing purposes: for ($i = 2; $i <= date('n'); $i++) echo date('M', mktime(0, 0, 0, $i - 1, 1)), ".\t", date('n/j', strtotime('+2 days', strtotime('last Friday', strtotime('first day of this month last month', mktime(0, 0, 0, $i, 1))))), '-', date('n/j', strtotime('+1 day', strtotime('last Friday', strtotime('first day of this month', mktime(0, 0, 0, $i, 1))))), '<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277025 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 Cool. How reliable is strtotime? How does this code look? It seems to work well. If you see something I should alter, please let me know. $sunday=date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday', strtotime('now')))); $saturday=date('Y-m-d', strtotime('last Saturday', strtotime('now'))); $firstday=date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month')))); $lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))); echo"$sunday<br />$saturday<hr />"; echo"$firstday<br />$lastday"; Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277037 Share on other sites More sharing options...
silkfire Posted October 7, 2011 Share Posted October 7, 2011 Never use strtotime('now') it's completely redundant. strtotime always assumes "now" which in PHP is written as the return of function time(). $sunday = date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday'))); $saturday = date('Y-m-d', strtotime('last Saturday')); $firstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month')))); $lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))); echo"$sunday<br />$saturday<hr />"; echo"$firstday<br />$lastday"; Not sure though those are the dates you wanted. See my previous code. Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277044 Share on other sites More sharing options...
jakebur01 Posted October 7, 2011 Author Share Posted October 7, 2011 Thank you for your help. Those were the correct dates. Quote Link to comment https://forums.phpfreaks.com/topic/248576-working-with-dates/#findComment-1277053 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.