-1

I have do a custom page with a planning of the actual week, and the week is defined by the today's day.

I need to put link for previous and next week, and i want to make a link to reopen my page with today'day + 7 (for next) or -7 (for previous).

This is the function for defined days of the week with the today's day var.

So i need to make a link in the page to go on previous week (same page with $today -7) and another to go on next week (same page with $today +7).

Can you help me ? thanks a lot.

EDIT : I have try to adapt the Michal's solution and delete my old poo function for replace procedural code :

<?php 

date_default_timezone_set('Europe/Paris');

if (!empty($_GET['today'])) 
{
    $today = $_GET['today'];
}

else
{
    $today = Date('y-m-d'); 
}

$todayMinus7 = Date('y-m-d', strtotime("-7 days")); //set variable to last week (-7 days)
$todayPlus7 = Date('y-m-d', strtotime("+7 days"));  //set variable to next week (+7 days)


$my_date = $today; 
$week = date("W", strtotime($my_date)); // get week
$y =    date("Y", strtotime($my_date)); // get year

$first_date =  date('y-m-d',strtotime($y."W".$week)); //first date 
$second_date = date("y-m-d",strtotime("+1 day", strtotime($first_date)));

?>

<a href="get_day.php?today=<?php echo $todayPlus7; ?>">A Week Ago</a>

<?php   echo $first_date;  ?>

RESULT :

Now when i load the page i got the first_date (monday) 18/10/08, thats ok !

if i press on the link i have the next monday 18/10/15, thats's ok !

But if i click on the link once again (for go on next week of the previous next week) nothing changes (always 18/10/15 instead of 18/10/22).

Do you an idea for solve the problem ?

Thanks a lot,

Sanalol
  • 57
  • 1
  • 6
  • 1
    Use date('W'); for current week. and +1 or -1 for previous and next week. There are always 52 weeks in the year. Also explain how you are supposed to use getDay() – Ibu Oct 06 '18 at 20:19
  • Hi and thanks for you help, i use getDay() to create the date of every day of the week, for example : echo getDay('Monday')->format('d/m/y'); to show 1/10/18 under the "monday" div. So how i can modify the $today->formate('W') ? Thanks a lot for your help ! – Sanalol Oct 07 '18 at 12:02

2 Answers2

1

I have something similar on my page, I have created different variables with dates and then just use them whenever I need to... So create:

<?php
$todayMinus7 = Date('y-m-d', strtotime("-7 days")); //set variable to last week (-7 days)
$today = Date('y-m-d');                             //set variable to today
$todayPlus7 = Date('y-m-d', strtotime("+7 days"));  //set variable to next week (+7 days)
$dayName = !empty($_GET['today']) ? date('l',$_GET['today']) : date('l',$today); ; //shorthand for IF today is set, get day name
?>

Then have you links where you need them and add above variable to the links like so:

<a href="get_day.php?today=<?php echo $todayPlus7.'">'.$dayName;?></a>
michal
  • 327
  • 4
  • 15
  • Hi and thanks for your answers, i have try your solution but it doesn't work, i think i need to modify something, i have edit my post with your solution and the result. Do you have an idea ? Thanks Michal ! – Sanalol Oct 07 '18 at 12:10
  • Hi Michal and thanks for your help, i have try to delete my declarationand replace by yours before get_day() function and change the link but i have two error : Notice: A non well formed numeric value encountered in C:\wamp64\www\get_day.php on line 13 and always Call to a member function setISODate() on string in C:\wamp64\www\get_day.php on line 20. i think the syntaxe on poo was different than procedural with strtotime. do you have an idea ? thanks for your help – Sanalol Oct 07 '18 at 21:53
  • I have try to adapte your code with procedural code instead of my poo function and thats better, but i don't know why i can't go on next week of next week, do you have an idea ? Thanks a lot for your help, i have try lot of time today to find the solution. – Sanalol Oct 07 '18 at 23:21
0

So i have do this and it's work great :

<?php 
date_default_timezone_set('Europe/Paris');
if (!empty($_GET['today'])) 
{
    $today = $_GET['today'];
}
else
{
    $today = Date('Y-m-d'); 
}
$todayMinus7 = Date('Y-m-d', strtotime("-7 days", strtotime($today))); 
$todayPlus7 = Date('Y-m-d', strtotime("+7 days", strtotime($today)));  

$my_date = $today; 
$week = date("W", strtotime($my_date)); // get week
$y =    date("Y", strtotime($my_date)); // get year
$monday =  date('m-d-Y',strtotime($y."W".$week)); //first date 
$tuesday = date("m-d-Y",strtotime("+1 day", strtotime($monday)));
$wednesday = date("m-d-Y",strtotime("+2 day", strtotime($monday)));
$thursday = date("m-d-Y",strtotime("+3 day", strtotime($monday)));
$friday = date("m-d-Y",strtotime("+4 day", strtotime($monday)));
$saturday = date("m-d-Y",strtotime("+5 day", strtotime($monday)));
$sunday = date("m-d-Y",strtotime("+6 day", strtotime($monday)));
?>

<a href="get_day.php?today=<?php echo $todayMinus7; ?>">Semaine précédente</a>
<a href="get_day.php?today=<?php echo $todayPlus7; ?>">Semaine suivante</a>

<?php   echo $monday." <br/>".$tuesday." <br/>".$wednesday." <br/>".$thursday."  <br/>".$friday." <br/>".$saturday." <br/>".$sunday;  ?>

Thanks a lot Michal for your help !

Sanalol
  • 57
  • 1
  • 6