Page 1 of 1

How to calculate starting date in week where week is known

Posted: Thu Feb 10, 2005 9:47 pm
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!

Posted: Thu Feb 10, 2005 10:21 pm
by feyd
1) given a timestamp, date can tell you the week number.
2) play with strtotime

Posted: Thu Feb 10, 2005 10:30 pm
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?

Posted: Thu Feb 10, 2005 10:48 pm
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;

Posted: Fri Feb 11, 2005 12:55 pm
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?

Posted: Fri Feb 11, 2005 1:06 pm
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.

Posted: Fri Feb 11, 2005 4:40 pm
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?