Jump to content

get start and end date of the following week


jarvis

Recommended Posts

Hi,

 

I'm trying to alter the code below. It cuurently finds the start and end date of this week, no matter what day you're on.

 

What I now need is to find the start & end date of next week, again, no matter what day you're on of this week

 

Thanks to aleX_hill for the following code:

<?php
#THIS WEEK
$dayOfWeek = date("w"); #The number of the day of the week. Sun = 0, Sat = 1
$todayDayOfMonth = date("d"); #The day of the month
$thisMonth = date("m"); #The number of this month. Jan = 1, Dec = 12

$firstDayOfWeek = $todayDayOfMonth - $dayOfWeek; #The day of the month for the beginning of the week (Sun)
if($firstDayOfWeek < 1) #The beginning of the week was in the previous month
{
$monthBeginWeek = $thisMonth - 1;
$firstDayOfWeek = $firstDayOfWeek + date("t",mktime(0,0,0,$monthBeginWeek,1,date("Y")));
} else {
$monthBeginWeek = $thisMonth;
}
$lastDayOfWeek = $todayDayOfMonth + (6 - $dayOfWeek); #Get the day of the month of the end of the week (sat)
if($lastDayOfWeek > date("t")) #if the day of the month is larger then the number of days in the month
{
$monthEndWeek = $thisMonth + 1;
$lastDayOfWeek = $lastDayOfWeek - date("t"); #Then take the number of days in the week to get the day number of the next week
} else {
$monthEndWeek = $thisMonth;
}
$start_of_week = date("Y-m-d",mktime(0,0,0,$monthBeginWeek,$firstDayOfWeek,date("Y")));
#echo $start_of_week;
$end_of_week = date("Y-m-d",mktime(0,0,0,$monthEndWeek,$lastDayOfWeek,date("Y")));
#echo $end_of_week;
?>

 

 

Any help is much appreciated!

 

TIA

Link to comment
Share on other sites

Modifying your code...

 

<?php
#THIS WEEK

# ONE WEEK FROM TODAY
$ts2 = time() + 60*60*24*7;



$dayOfWeek = date("w", $ts2); #The number of the day of the week. Sun = 0, Sat = 1
$todayDayOfMonth = date("d", $ts2); #The day of the month
$thisMonth = date("m", $ts2); #The number of this month. Jan = 1, Dec = 12

$firstDayOfWeek = $todayDayOfMonth - $dayOfWeek; #The day of the month for the beginning of the week (Sun)
if($firstDayOfWeek < 1) #The beginning of the week was in the previous month
{

$monthBeginWeek = $thisMonth - 1;

$firstDayOfWeek = $firstDayOfWeek + date("t",mktime(0,0,0,$monthBeginWeek,1,date("Y",$ts2)));
} else {

$monthBeginWeek = $thisMonth;
}
$lastDayOfWeek = $todayDayOfMonth + (6 - $dayOfWeek); #Get the day of the month of the end of the week (sat)
if($lastDayOfWeek > date("t", $ts2)) #if the day of the month is larger then the number of days in the month
{

$monthEndWeek = $thisMonth + 1;

$lastDayOfWeek = $lastDayOfWeek - date("t", $ts2); #Then take the number of days in the week to get the day number of the next week
} else {

$monthEndWeek = $thisMonth;
}
$start_of_week = date("Y-m-d",mktime(0,0,0,$monthBeginWeek,$firstDayOfWeek,date("Y", $ts2)));
echo $start_of_week  . "<br>";
$end_of_week = date("Y-m-d",mktime(0,0,0,$monthEndWeek,$lastDayOfWeek,date("Y", $ts2)));
echo $end_of_week;
?>

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.