Jump to content

[SOLVED] Display a week of dates


The Little Guy

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/123703-solved-display-a-week-of-dates/
Share on other sites

// 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>
';
}

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');

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

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;
}


?>

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.