Jump to content

Making a Week 1 or Week 2


dean7

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>");
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.