How to calculate starting date in week where week is known

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

How to calculate starting date in week where week is known

Post by dardsemail »

Hi,

I have a little calendar application that I'm building and I have built in a nice little javascript forward and back button. I am able to calculate the week without a problem, but am having difficulty in changing the weekly data for that week.

My thinking is that if I can calculate the dates that are within that week, that might be the ticket. I'm not sure if that's the best way to go about it - but if anyone can provide insight on either:

1) the way to determine which dates are within a given week number in a particular year

or

2) a better way to calculate the dates moving forward or backward a week

it would be very much appreciated!!!

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

1) given a timestamp, date can tell you the week number.
2) play with strtotime
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I actually have the week number.. is there anyway to convert that back into a date format somehow to be able to calculate the days that are in that week # for a given year?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you've already got the timestamp to get the week number, why waste time getting the timestamp again? You can use date() and strtotime() to find the start of the week (varies by country) like this (untested)

Code: Select all

$start_time = strtotime( ($days_diff = (date('w', $time) - $start_of_week)) ? (($days_diff > 0) ? '+' : '') . $days_diff . ' days ' : '') . '00:00:00' );
$end_time = $start_time + 8 * 24 * 60 * 60 - 1;
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I'm totally baffled by that last posting... sorry!

I've been trying with this to no avail:

week is a number passed from the URL.

Code: Select all

$week=$_GETї'week'];
			echo $week."  ";
			$selWeek = strtotime($week);
			echo $selWeek;
			$selWeek = date("m.d.y",$selWeek);
			echo $selWeek;
The week # is coming through fine... and the $selWeek value seems to be changing, but the date that appears is always today's date.

I want to try and have it pick the date based on the week value. Is that possible?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you're just passing $week to strtotime() it'll likely always think it's sometime today.. without a reference.. read how strtotime works.
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I read it and what i'm getting is that it seems that there is a way to determine what the particular week number is for a given date, but there doesn't seem to be a (simple) way to extract the dates for a given week number.

Is this accurate?
Post Reply