Codemunkie Posted May 26, 2011 Share Posted May 26, 2011 Hello there! I've been at this for too long! I've got a normal calendar at the side of my website that works fine, however i would like one that shows the next 7 days starting at today. I have an image here that kind of shows what i want... So it will show the next 7 days starting with today, and going into the next month if todays date is the 30th etc. Could anybody please help me? :\ Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/ Share on other sites More sharing options...
Maq Posted May 26, 2011 Share Posted May 26, 2011 date and strtotime are probably what you're going to need here. Do you have any code for us to work off of? Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220660 Share on other sites More sharing options...
Codemunkie Posted May 26, 2011 Author Share Posted May 26, 2011 <?php $cDay = date("d"); echo '<tr>'; for ($i=$cDay; $i<($cDay+7); $i++) { echo '<td width="14%" class="calender1a"><b>Sunday</b></td>'; } echo '</tr>'; echo '<tr>'; for ($i=$cDay; $i<($cDay+7); $i++) { echo '<td width="14%" class="calender2a">Raid<br />Time<br />Picture?</td>'; } echo '</tr>'; ?> Thats the code so far, the only thing i can't do is make it show the day names at the top... Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220664 Share on other sites More sharing options...
Maq Posted May 26, 2011 Share Posted May 26, 2011 Thats the code so far, the only thing i can't do is make it show the day names at the top... Try something like: $cDay = strtotime(date("d")); for($i=1; $i{ echo '' . date("l", strtotime('+'.$i.'day', $cDay)) . ''; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220672 Share on other sites More sharing options...
Codemunkie Posted May 26, 2011 Author Share Posted May 26, 2011 Loading my page with that code completely crashes my web browser :L No idea why! Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220674 Share on other sites More sharing options...
Maq Posted May 26, 2011 Share Posted May 26, 2011 Loading my page with that code completely crashes my web browser :L No idea why! Crashes it or shows a blank page? It works for me (ff3). Output (of course I don't have your CSS class): ThursdayFridaySaturdaySundayMondayTuesdayWednesday If it's blank, try turning on error reporting: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220677 Share on other sites More sharing options...
Codemunkie Posted May 26, 2011 Author Share Posted May 26, 2011 Nevermind, there was an error in my code because the $cDay variable changed :L Works perfectly, thank you so much! Would you mind explaining a little how you made it work? Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220679 Share on other sites More sharing options...
Maq Posted May 26, 2011 Share Posted May 26, 2011 Comments in the code (check out date for date formatting, the "l"): //Gets the current day (Thursday) $cDay = strtotime(date("d")); //Loop through 1-7, to increment each day of the week for($i=1; $i{ //"l" is just the format for date (A full textual representation of the day of the week) //strtotime takes the current day ($cDay) and adds +1 ($i) each loop, adding a day echo '' . date("l", strtotime('+'.$i.'day', $cDay)) . ' $i -> +' . $i . 'day '; } ?> Output: Thursday $i-> +1day Friday $i-> +2day Saturday $i-> +3day Sunday $i-> +4day Monday $i-> +5day Tuesday $i-> +6day Wednesday $i-> +7day Hope that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220687 Share on other sites More sharing options...
xyph Posted May 26, 2011 Share Posted May 26, 2011 strtotime converts a date in string format to a unix timestamp. the for loop pretty much makes sure you loop 7 times strtotime('+'.$i.'day', $cDay) will translate to strtotime('+1/2/3... day', currentTimestamp) which will return a unix timestamp 1,2,3,etc days from now. The date('l') returns the textual day of the week from that timestamp. The only thing I don't like about this function is that date is a notoriously slow function, from what I remember. It could be sped up, potentially with something like this $today = date('l'); $days = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $split = array_search($today,$days); $days = array_merge(array_slice($days,$split),array_slice($days,0,$split)); echo implode(', ', $days); I'm honestly not sure if it's much faster that Maqs script, which is much more simple in design. His will be much better if you also want to echo the day of the month. Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220697 Share on other sites More sharing options...
Maq Posted May 26, 2011 Share Posted May 26, 2011 The date functions are a bit slow but for only displaying 7 days of the week and the benefit of having the flexibility of changing/adding to the format, I think it's worth it. Quote Link to comment https://forums.phpfreaks.com/topic/237544-calendar-help/#findComment-1220700 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.