Jump to content


Photo

Script that generates the next few Tues/Thurs pairs and stores them in a numbered array


  • Please log in to reply
3 replies to this topic

#1 purefusion

purefusion
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 14 March 2006 - 03:49 AM

Hey, I'm looking for a simple script that generates the next few Tues/Thurs pair combinations and stores them in a numbered array. Simple enough?

For example: If today is Monday the 13, array[1] = Tuesday14, array[2] = Thursday16, etc
I would like to have this rule: if today is Tuesday already, start with the next Thursday, and vice-versa.

They will be echoed in a loop that generates a drop down box in a form such as:

    <select name="select">
      <option value="tues14">Tuesday</option>
      <option value="thurs16">Thursday</option>
      < ... etc ... >
    </select>

Hoping you can point me along in the direction of simple coding, as something I'd come up with would to too over-coded and would probably take forever to render.

Thanks so much!

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 March 2006 - 08:02 AM

try

$today = mktime(0,0,0);
$arr = array();

for ($d=1; $d<=30; $d++) {
     $dt = strtotime("+$d days", $today);
     //get day of week
     $dow = date('w', $dt);
     // store dateval in array
     if (($dow==2) || ($dow==4)) $arr[] = $dt;
}

// print options
echo "<SELECT name='days'>\n";
foreach ($arr as $dt) {
         printf ("<option value='%s'>%s</option>\n", date('Dd', $dt), date('l j', $dt));
}
echo "</SELECT>\n";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 purefusion

purefusion
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 14 March 2006 - 12:54 PM

Works beautifully. I will gleen from it. Barand, you rock!

#4 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 14 March 2006 - 06:53 PM

That's simple, but does a little extra work. As soon as you find one, most of your work is done. Shouldn't be a big deal either way though.

$today = mktime(0,0,0);
$arr = array();
$adv = 1;

for ($d=1; $d<=30; $d += $adv) {
     $dt = strtotime("+$d days", $today);
     //get day of week
     $dow = date('w', $dt);
     // store dateval in array
     if (($dow==2) || ($dow==4)) $arr[] = $dt;
     if ($dow == 2) $adv = 2;
     if ($dow == 4) $adv = 5;
}

// print it like Barand showed you





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users