Jump to content

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


?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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