Jump to content

week date range in calendar pagination increment


hfheuhf

Recommended Posts

I have the week date range displaying the week range from sunday - saturday

 

$current_dayname = date("0"); // return sunday monday tuesday etc.
echo $date = date("Y-m-d",strtotime('last sunday')).'to'.date("Y-m-d",strtotime("next saturday"));

 

Which outputs in the following format 2015-01-25to2015-01-31

 

However, when I press next or previous, the dates don't change.

 

<?php
$year = (isset($_GET['year'])) ? $_GET['year'] : date("Y");
$week = (isset($_GET['week'])) ? $_GET['week'] : date('W');
if($week > 52) {
    $year++;
    $week = 1;
} elseif($week < 1) {
    $year--;
    $week = 52;
}
?>

<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week == 52 ? 1 : 1 + $week).'&year='.($week == 52 ? 1 + $year : $year); ?>">Next  Week</a> <!--Next week-->
<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week == 1 ? 52 : $week -1).'&year='.($week == 1 ? $year - 1 : $year); ?>">Pre Week</a> <!--Previous week-->

<table border="1px">
    <tr>
        <td>user</td>
<?php
if($week < 10) {
    $week = '0'. $week;
}
for($day= 1; $day <= 7; $day++) {
    $d = strtotime($year ."W". $week . $day);

    echo "<td>". date('l', $d) ."<br>". date('d M', $d) ."</td>";
}
?>
    </tr>
</table>

Link to comment
Share on other sites

If I manually create a date with strtotime(), I believe I get what you're looking for. So, my guess is that your variables do not hold the values that you think they do. Try inspecting them before you call strtotime() from the loop.

 

Also, please do not do this: <a href="<?php echo $_SERVER['PHP_SELF']

 

This is not safe! You can simply link the query string and it will be attached to the current page.

 

<a href="?week='.($week == 52 ? 1 : 1 + $week).'&year='.($week == 52 ? 1 + $year : $year); ?>">Next  Week</a>
Link to comment
Share on other sites

 

If the week changes then the week start and end of week dates will need to be changed by +/- 7 days

$wkstart = '2015-01-18';
$wkstart = date('Y-m-d', strtotime("$wkstart +7 days"));
echo $wkstart;  //--> 2015-01-25

Sorry that doesn't work, well it shows the date it just doesn't change when i press next or previous

what im trying to do is i have a calendar and i am trying to put the week range dates at the top

Link to comment
Share on other sites

If I manually create a date with strtotime(), I believe I get what you're looking for. So, my guess is that your variables do not hold the values that you think they do. Try inspecting them before you call strtotime() from the loop.

 

Also, please do not do this: <a href="<?php echo $_SERVER['PHP_SELF']

 

This is not safe! You can simply link the query string and it will be attached to the current page.

 

<a href="?week='.($week == 52 ? 1 : 1 + $week).'&year='.($week == 52 ? 1 + $year : $year); ?>">Next  Week</a>

i never knew that, it's always in php books and everythin could you please tell me why, ill change it asap

Link to comment
Share on other sites

i never knew that, it's always in php books and everythin could you please tell me why, ill change it asap

Yes, unfortunately there is a lot of dangerous misinformation floating around out there.

 

The problem is that users can manipulate the value of PHP_SELF, which creates an XSS vulnerability.

 

In fact, your other variables are potentially dangerous as well (for the same reason), since you are not sanitizing them before re-displaying them in your page.

Link to comment
Share on other sites

Yes, unfortunately there is a lot of dangerous misinformation floating around out there.

 

The problem is that users can manipulate the value of PHP_SELF, which creates an XSS vulnerability.

 

In fact, your other variables are potentially dangerous as well (for the same reason), since you are not sanitizing them before re-displaying them in your page.

thank you, i will consider this. by any chance do you know how to get start and end days for a week number in php where Sunday is the start day instead of monday??

 

 

 
Link to comment
Share on other sites

Something along these lines

$year = date('Y');
$week = 4;

$week -= 1;  // zero-based wk no
$dayno = $week * 7;
$dt = DateTime::createFromFormat("Y-z", "{$year}-{$dayno}");

$dt1 = new DateTime("Last Sunday ".$dt->format('Y-m-d'));
$dt2 = new DateTime("Next Saturday ".$dt->format('Y-m-d'));

echo $dt1->format('D Y-m-d').'<br>';  //--> Sun 2015-01-18
echo $dt2->format('D Y-m-d').'<br>';  //--> Sat 2015-01-24
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.