hfheuhf Posted January 22, 2015 Share Posted January 22, 2015 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><?phpif($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> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 22, 2015 Share Posted January 22, 2015 What are you getting back from your urls? Have you looked? Quote Link to comment Share on other sites More sharing options...
hfheuhf Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) The url shows the current weeks when you click prev or next, so currently is week 4, if i click again it shows week 5. However, the week date range doesn't change along with the next/previous Edited January 22, 2015 by hfheuhf Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 22, 2015 Share Posted January 22, 2015 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> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
hfheuhf Posted January 22, 2015 Author Share Posted January 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
hfheuhf Posted January 22, 2015 Author Share Posted January 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 23, 2015 Share Posted January 23, 2015 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. Quote Link to comment Share on other sites More sharing options...
hfheuhf Posted January 23, 2015 Author Share Posted January 23, 2015 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?? Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 23, 2015 Share Posted January 23, 2015 I believe you can achieve that if you use day 0 instead of day 1. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 23, 2015 Share Posted January 23, 2015 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.