The Little Guy Posted September 11, 2008 Share Posted September 11, 2008 How can I make a loop, and display dates starting with the current weeks Monday and ending on current weeks Sunday. Example: Today = 09/10/2008 The dates should look like this: Mon = 09/08/2008 Tue = 09/09/2008 Wed = 09/10/2008 Thu = 09/11/2008 Fri = 09/12/2008 Sat = 09/13/2008 Sun = 09/14/2008 Quote Link to comment https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/ Share on other sites More sharing options...
Lodius2000 Posted September 11, 2008 Share Posted September 11, 2008 add 1 to date() (where date has the date how you want it formatted in there)... then use a while((date()++) < { print date(); } // this will give you something to chew on, give us some code and we will get more specific Quote Link to comment https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/#findComment-638761 Share on other sites More sharing options...
The Little Guy Posted September 11, 2008 Author Share Posted September 11, 2008 // Get the First day of the week switch(date("N")){ case 1:$date = 0;break; case 2:$date = -1;break; case 3:$date = -2;break; case 4:$date = -3;break; case 5:$date = -4;break; case 6:$date = -5;break; case 7:$date = -6;break; } $firstDay = date("m/d/Y",strtotime($date.' days')); // Set the output of the first day of the week // Here is where I am not sure what to do: for($i=1;$i<8;$i++){ echo' <th>'.date("m/d/Y",strtotime($firstDay)).'</th> '; } Quote Link to comment https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/#findComment-638764 Share on other sites More sharing options...
discomatt Posted September 11, 2008 Share Posted September 11, 2008 Or replace switch(date("N")){ case 1:$date = 0;break; case 2:$date = -1;break; case 3:$date = -2;break; case 4:$date = -3;break; case 5:$date = -4;break; case 6:$date = -5;break; case 7:$date = -6;break; } with $date = 1 - date('N'); Quote Link to comment https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/#findComment-638769 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 Here is an excerpt from the PHP Manual on the date function here is the simpliest way to get the start and end date of the week; <?php $sdate=date('c',strtotime(date('Y')."W".date('W')."0")); $edate=date('c',strtotime(date('Y')."W".date('W')."7")); ?> the format is for the string in strtotime is; 2008W200 this stands for year - 2008, constant never changes - W, week number of the year - 20, day of the week - 0 for sunday, 1 for monday, etc.... so 2008W200 stands for the sunday of the 20th week of 2008. This will only work in php 5 or better Quote Link to comment https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/#findComment-638782 Share on other sites More sharing options...
discomatt Posted September 11, 2008 Share Posted September 11, 2008 Here's another using timestamps <?php $startMonday = 1; # Change this to 0 to have the week start on Sunday $now = strtotime( ($startMonday-date('N')).' days' ); $end = $now + ( 60*60*24*7 ); while( $now < $end ) { echo date( 'm/d/Y', $now ) . '<br />'; $now += 60*60*24; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/#findComment-638787 Share on other sites More sharing options...
The Little Guy Posted September 11, 2008 Author Share Posted September 11, 2008 discomatt, that worked great! We will see if its still working next week! Quote Link to comment https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/#findComment-638841 Share on other sites More sharing options...
discomatt Posted September 11, 2008 Share Posted September 11, 2008 You could even remove the strtotime call if you really wanted to using $now = time() + ($startMonday-date('N')) *60*60*24; Quote Link to comment https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/#findComment-638848 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.