Jump to content


Photo

First and Last date of a week


  • Please log in to reply
8 replies to this topic

#1 smith.james0

smith.james0
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 01 May 2006 - 04:16 PM

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

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 01 May 2006 - 05:26 PM

You looking for something for just the current year??

#3 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 01 May 2006 - 05:33 PM

This might help you... but you'll have to have access to enable the extension:
[a href=\"http://us3.php.net/manual/en/ref.mcal.php\" target=\"_blank\"]http://us3.php.net/manual/en/ref.mcal.php[/a]

Info: PHP Manual


#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 01 May 2006 - 05:39 PM

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.

<?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>";
?>

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

Ray

#5 smith.james0

smith.james0
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 01 May 2006 - 05:48 PM

craygo, I only need the one week at a time.

$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";
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

#6 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 01 May 2006 - 06:11 PM

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.


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

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

Ray

#7 smith.james0

smith.james0
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 01 May 2006 - 06:29 PM

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.


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

Thanks James

#8 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 01 May 2006 - 06:54 PM

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

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

Ray

#9 smith.james0

smith.james0
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 01 May 2006 - 09:10 PM

Thanks, that works great!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users