#### Archived

This topic is now archived and is closed to further replies.

# First and Last date of a week

## Recommended Posts

I have been searching the web to find a script which, will tell me the date of the first day(Sunday) and last(Saturday) of the week, if I give it a week number.
Ie Week 18 = 30/04/06 (Sunday) 05/05/06 (Saturday)

Can anyone help?

Thanks James

##### Share on other sites
You looking for something for just the current year??

##### Share on other sites
[a href=\"http://us3.php.net/manual/en/ref.mcal.php\" target=\"_blank\"]http://us3.php.net/manual/en/ref.mcal.php[/a]

##### Share on other sites
I have to give props to barand. Found some of this in another forum and modified it for this particular case. Although this code only seems to work for the current year. Will check to get it to work for any year.

[code]<?php
// Get current year
\$y = date('Y');
// Get first month (january)
\$m = date('n', strtotime('January 1'));
// Get first day of January
\$d = date('j', strtotime('January 1'));
// set week value here
\$week_var = '1';
// Take away 1 Since the array starts with 0 and not 1
\$week_num = \$week_var - 1;

\$weeks = 52; // How many weeks 52 for full year
// Start array for sunday dates
\$sundays = array();
// Put sunday dates into the array
for (\$w = 0; \$w < \$weeks; \$w++) \$sundays[] = date('d M y', mktime(0,0,0,\$m,\$d + \$w*7,\$y));
// Loop thru array to search for week
foreach(\$sundays as \$key => \$value){
// If the key = your week...
if(\$key == \$week_num){
// Set Sunday and Saturday dates when found
\$sunday = date("m-d-Y", strtotime(\$value));
\$saturday = date("m-d-Y", strtotime("\$value +6 days"));
// Print to see results
print 'Sunday = '.\$sunday.'  Saturday = '.\$saturday.'<br>';
}
}
// Print entire array (for testing only)
echo "<pre>";
print_r (\$sundays);
echo "</pre>";
?>[/code]

Again thanks to barand for posting some of this in phpbuilder forum, just added my own code to search thru array.

Ray

##### Share on other sites
craygo, I only need the one week at a time.

[code]\$StartOfWeek = date("d",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")))."/".date("m",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")))."/".date("Y",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")));

\$EndOfWeek = date("d",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y")))."/".date("m",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y")))."/".date("Y",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y")));

echo "\$StartOfWeek to \$EndOfWeek";
[/code]
The problem with this is it only works for the current week, I need to be able to tell it a week and it give me the dates back.

Thanks James

##### Share on other sites
OK I was going a little too deep. You can use this so if you are looking for a particular week in any year. This will do what you want. You need the loop to store all the dates so you have something to search for.

[code]<?php
// Set year to use
\$y = '2006';
// Get date of first sunday
\$firstweek = date("Y-m-d", strtotime("sunday january \$y"));
// Get first month (january)
\$m = date('n', strtotime(\$firstweek));
// Get first Sunday in January
\$d = date('j', strtotime(\$firstweek));

// set week value here
\$week_var = '1';  <---this is where you give the week number
// Take away 1 Since the array starts with 0 and not 1
\$week_num = \$week_var - 1;

\$weeks = 52; // How many weeks 52 for full year
// Start array for sunday dates
\$sundays = array();
// Put sunday dates into the array
for (\$w = 0; \$w < \$weeks; \$w++) \$sundays[] = date('d M y', mktime(0,0,0,\$m,\$d + \$w*7,\$y));
// Loop thru array to search for week
foreach(\$sundays as \$key => \$value){
// If the key = your week...
if(\$key == \$week_num){
// Set Sunday and Saturday dates when found
\$StartOfWeek = date("m-d-Y", strtotime(\$value));
\$EntOfWeek = date("m-d-Y", strtotime("\$value +6 days"));
// Print to see results
echo "\$StartOfWeek to \$EndOfWeek";
}
}
?>[/code]

If you are passing a variable thru a form or url set the \$week_var to the parameter needed
[code]\$week_var = \$_GET['weeknum'];
OR
\$week_var = \$_POST['weeknum'];[/code]

Ray

##### Share on other sites
I have just tried this using week 18 as a test, and it wont work. I just get a blank screen. I have looked through it a couple of time but carn't see anything out of place.

[code]<?php

\$week_var = '18';

// Set year to use
\$y = '2006';
// Get date of first sunday
\$firstweek = date("Y-m-d", strtotime("sunday january \$y"));
// Get first month (january)
\$m = date('n', strtotime(\$firstweek));
// Get first Sunday in January
\$d = date('j', strtotime(\$firstweek));

// set week value here
\$week_var = '1';  <---this is where you give the week number
// Take away 1 Since the array starts with 0 and not 1
\$week_num = \$week_var - 1;

\$weeks = 52; // How many weeks 52 for full year
// Start array for sunday dates
\$sundays = array();
// Put sunday dates into the array
for (\$w = 0; \$w < \$weeks; \$w++) \$sundays[] = date('d M y', mktime(0,0,0,\$m,\$d + \$w*7,\$y));
// Loop thru array to search for week
foreach(\$sundays as \$key => \$value){
// If the key = your week...
if(\$key == \$week_num){
// Set Sunday and Saturday dates when found
\$StartOfWeek = date("m-d-Y", strtotime(\$value));
\$EntOfWeek = date("m-d-Y", strtotime("\$value +6 days"));
// Print to see results
echo "\$StartOfWeek to \$EndOfWeek";
}
}
?>
[/code]

Thanks James

##### Share on other sites
there is already a spot for the week number. take the one out you used at the top and go down to line #11 and change the week there. Copy and paste this code

[code]<?php
// Set year to use
\$y = '2006';
// Get date of first sunday
\$firstweek = date("Y-m-d", strtotime("sunday january \$y"));
// Get first month (january)
\$m = date('n', strtotime(\$firstweek));
// Get first Sunday in January
\$d = date('j', strtotime(\$firstweek));

// set week value here ****the week number goes below****
\$week_var = '18';
// Take away 1 Since the array starts with 0 and not 1
\$week_num = \$week_var - 1;

\$weeks = 52; // How many weeks 52 for full year
// Start array for sunday dates
\$sundays = array();
// Put sunday dates into the array
for (\$w = 0; \$w < \$weeks; \$w++) \$sundays[] = date('d M y', mktime(0,0,0,\$m,\$d + \$w*7,\$y));
// Loop thru array to search for week
foreach(\$sundays as \$key => \$value){
// If the key = your week...
if(\$key == \$week_num){
// Set Sunday and Saturday dates when found
\$StartOfWeek = date("m-d-Y", strtotime(\$value));
\$EntOfWeek = date("m-d-Y", strtotime("\$value +6 days"));
// Print to see results
echo "\$StartOfWeek to \$EndOfWeek";
}
}
?>[/code]

Ray

##### Share on other sites
Thanks, that works great!