Jump to content

First and Last date of a week


smith.james0

Recommended Posts

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]
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

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.