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?

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.