dean7 Posted October 13, 2016 Share Posted October 13, 2016 Hey guys I'm trying to code some code which gives me a week 1 or a week 2. So for example, this week is week one, next week is week two the following week after would be week one again. How can I actually code this? Would it be an ideal way to maybe get the week number and if the week number is Even its Week 1 if its Odd its Week 2? or is there a simple way I could go about this? Thanks for any advice! Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/ Share on other sites More sharing options...
ginerjm Posted October 13, 2016 Share Posted October 13, 2016 You really need to describe this better. Is this week right now #1 or #2? And what makes it that? When did this week begin? When does it end? How does it "end"? Why does it end? Why is it even numbered? Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/#findComment-1538259 Share on other sites More sharing options...
dean7 Posted October 13, 2016 Author Share Posted October 13, 2016 Yeah sorry its hard explaining what I want to achieve. Its dont matter what this week would start as. If this week is week one, next week would need to be week two, or the other way around. Week to start Sunday and end Saturday, so like Saturday at midnight it would change to the next week Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/#findComment-1538260 Share on other sites More sharing options...
ginerjm Posted October 13, 2016 Share Posted October 13, 2016 How are you keeping track of the "current week #"? In a db? Is it the same week for all users? Will you use a cron job to trigger the week change? Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/#findComment-1538261 Share on other sites More sharing options...
dean7 Posted October 13, 2016 Author Share Posted October 13, 2016 I'm not sure how to hold the current week and yeah the week will be the same for everyone Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/#findComment-1538262 Share on other sites More sharing options...
ginerjm Posted October 13, 2016 Share Posted October 13, 2016 I think a db would be perfect. A simple table could be setup with either: #1. A single record that simply states the week number. A 1-record table that just reports the week # in a field. Perhaps an additional field to record the datetime of the last change to ensure that nothing is wrong. This implies a function that does a simple query to get the week # from the single record. OR #2. A record setup for every week in the year having perhaps a start date and end date for the week with the week number assigned to each week. This eliminates any need for setting it every Sunday. You would just need a script that establishes the weeks for the coming year. This means you need a function to simply get the week for the given date in the function call. Use that date to do a select query where the arg_date >= 'start_date' and arg_date <= 'end_date' to get the current week #. Strongly suggest using a function that stands alone that you simply include in your script(s) to minimize the impact if you have to make later changes. Let the call use a date value to be used to find the week in option 2 Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/#findComment-1538263 Share on other sites More sharing options...
maxxd Posted October 13, 2016 Share Posted October 13, 2016 If I'm understand what you're trying to accomplish, you could do something like this: $dt = new DateTime('now', new DateTimeZone('America/New_York')); print("<p>This is the date: {$dt->format('F j, Y')}</p>"); print("<p>It's the {$dt->format('W')} week of the year</p>"); if( ((int) $dt->format('W')) % 2 == 0 ){ print("<p>The week is even</p>"); }else{ print("<p>Odd week, eh?</p>"); } Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/#findComment-1538264 Share on other sites More sharing options...
Barand Posted October 14, 2016 Share Posted October 14, 2016 Unfortunately you can have week 53 followed by week 1, so two consecutive odd week numbers. Safer to use the number of weeks elapsed from a fixed date in the past. Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/#findComment-1538273 Share on other sites More sharing options...
Barand Posted October 14, 2016 Share Posted October 14, 2016 This function will return week number 1 or 2 for a given date (today being in week 1 and changing each sunday) function myWeekNumber(DateTime $date) { $base = new dateTime('2000-01-01'); // Fixed Saturday date return ceil($date->diff($base)->days/7) % 2 + 1; } Quote Link to comment https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/#findComment-1538276 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.