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>

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>

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
  On 1/22/2015 at 10:45 PM, Barand said:

 

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

  On 1/22/2015 at 10:40 PM, scootstah said:

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

  On 1/22/2015 at 11:55 PM, hfheuhf said:

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.

  On 1/23/2015 at 12:21 AM, scootstah said:

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

 

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.