Jump to content

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
https://forums.phpfreaks.com/topic/302326-making-a-week-1-or-week-2/
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

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

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>");
}

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;
}

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.