Jump to content

Drop down list of... days in the current week


leszer

Recommended Posts

So I was wondering if anyone could come up with a good way of having a drop down box list the dates of the previous week starting from Saturday.  The dates would need to go from Saturday to the coming/current friday.

 

Or would I just be better off having it list the days 'Saturday' to 'Friday'.

 

Any thoughts?

$date = strtotime('-1 saturday');
do {
  echo date('l F jS, Y', $date) . "\n";
} while ( ($date += (60*60*24)) && $date <= strtotime('next friday') );

 

There's a bug in that. ON a Saturday it would create two weeks of values. I think you'd need to check if the current day is friday and use that instead of "next friday"

 

EDIT: No I got that backward. If it is a saturday then it needs to start on the current day. So, it is the $date value that needs to be modified.

So...

 

if (date(strtotime( 'saturday')))
    $date=strtotime('saturday');
else $date=strtotime('-1 saturday');

 

??

 

Edit: forgot to put php in code.

 

$date = strtotime('-1 saturday');
do {
  echo date('l F jS, Y', $date) . "\n";
} while ( ($date += (60*60*24)) && $date <= strtotime('next friday') );

 

There's a bug in that. ON a Saturday it would create two weeks of values. I think you'd need to check if the current day is friday and use that instead of "next friday"

 

EDIT: No I got that backward. If it is a saturday then it needs to start on the current day. So, it is the $date value that needs to be modified.

OK, I played around with that code a bit and run into other problems due to anomalies with the timestamps - daylight savings time would definitely be an issue.

 

I have done some testing and the following code appears to work. I tested with the start day being the first day of the period, within the period and the last day of the period. You should do some more extensive testing.

 

//If today is NOT saturday, set start date to last saturday,
//else set to today
$start_date = (date('N')!=6) ? strtotime('-1 saturday') : time();
//Create list of the seven days starting from start date
for($day=0; $day<7; $day++)
{
  $date = strtotime("+{$day} days", $start_date);
  echo date('l F jS, Y', $date) . "<br>\n";
}

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.