[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

DISPLAY ONLY MONDAYS DATES IN A DROP DOWN BOX

Hi
I need to design a drop down box that will show only the Mondays date of each week for a month ie.  April drop down list will default to (force the user to select a monday)
4 APRIL 2005
11 APRIL 2005
18 APRIL 2005
25 APRIL 2005
for each week.  the user must only be able to select the start of the week but my caclulations will be done for the whole week. mon - sun
Please help me asap.  much appreciated
rika
0
SarikaKev
Asked:
SarikaKev
  • 4
  • 2
  • 2
  • +1
1 Solution
 
kulifajCommented:
Don't know how your php code looks right now (maybe you should post it), but you can use someting like this:

<SELECT name="datecombo">
<?php
for ($day=1; $day<=31; $day++) {
   $date = getdate(mktime(0,0,0,$month,$day,$year));
   if ($date["wday"]==1 && $date["mon"]==$month) {
      print("<OPTION value=$day>$day ". $date["month"] ." ". $year);
}
?>
</SELECT>

Where $month and $year are numeric representation of current values for month and year.
0
 
snoyes_jwCommented:
Here's another way.  It's roughly the same idea as kulifaj's code, but might be a little easier for you to read when you come back to the code in 6 months:

$date = strtotime("1 April 2005");
$endDate = strtotime("+1 month", $date);
while ($date < $endDate) {
  if (date("l", $date) == "Monday") {
    print("<option value=...whatever
  }
  $date = strtotime("tomorrow", $date);
}
0
 
SarikaKevAuthor Commented:
Hi Kulifaj & snoyes
thanks so much for your feedback
now Kulifajs answer doesn't want to give me an output
snoyes seems to give me an output in seconds
how can i convert this to a date and also create a date selection combo box from this
eg:  4 april 2005
      11 april 2005
      18 april 2005  
the user must select one of the weeks from the combo
thanks so much
rika
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kulifajCommented:
For my code you should set the $month and $year variables before, like this:

<SELECT name="datecombo">
<?php
$month = 4;
$year = 2005;

for ($day=1; $day<=31; $day++) {
   $date = getdate(mktime(0,0,0,$month,$day,$year));
   if ($date["wday"]==1 && $date["mon"]==$month) {
      print("<OPTION value=$day>$day ". $date["month"] ." ". $year);
}
?>
</SELECT>
0
 
snoyes_jwCommented:
And for mine, do
print("<option value='" . date("d M Y", $date) . "'>" . date("d M Y", $date) . "</option>");
0
 
ThaSmartUnoCommented:
just for a couple ms of processing saved ...
if you use snoyes_jw's while loop ... instead of tomorrow use "+7 day" or days ... one of those

and if you use kulifaj's for loop ...
for ($day=1; $day<=31; $day++) {
   $date = getdate(mktime(0,0,0,$month,$day,$year));
   if ($date["wday"]==1 && $date["mon"]==$month) {
      print("<OPTION value=$day>$day ". $date["month"] ." ". $year);
      $day+=6;                                  // since 1 will be added at end of loop
   }                                                  // also kulifaj you forgot this }
}
0
 
snoyes_jwCommented:
>>if you use snoyes_jw's while loop ... instead of tomorrow use "+7 day" or days ... one of those
No, don't.  That would be ok once you landed on the first Monday of the month.  But until you find that Monday, you need to use tomorrow, or + 1 day.

This would be ok:

$date = strtotime("1 April 2005");
$endDate = strtotime("+1 month", $date);
while ($date < $endDate) {
  if (date("l", $date) == "Monday") {
    print("<option value=...whatever
    $date = strtotime("+1 week", $date);
  }
  else {
    $date = strtotime("tomorrow", $date);
  }
}
0
 
ThaSmartUnoCommented:
yea good call, that's what i meant =)
0
 
SarikaKevAuthor Commented:
This works perfectly thanks alot
one last question
i want the date to default to the previous week
eg    4 April 2005
I tried using $date = strtotime('sysdate') but it defaults to 5 jan 1970
how can i do this ie make the date = sysdate - 7 days (it will always be the previous mon of the current date)
after this i will close the question! thanks
rika
0
 
snoyes_jwCommented:
$date = strtotime("-7 days");
$date = strtotime("-1 week");
Both will be 7 days back from today.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now