[sf-perl] That's not Perl!

Rich Morin rdm at cfcl.com
Tue Jan 17 07:49:44 PST 2006


I recently started using PHP for assorted odd jobs (see
http://www.cfcl.com/~rdm/weblog/archives/000910.html).  I
can't say that I like it as well as either Perl or Ruby
(a bit too quirky), but it has its uses.

Here's some code I hacked up for the Beer & Scripting SIG
page (http://www.cfcl.com/rdm/bass/), so that I wouldn't
have to keep editing the date line, making lame editing
mistakes, etc.  Comments welcome...

-r


% cat index.php
<?
  include '__intro';

  dt(dse(4, 3), '8:00 pm');  # 4th Wednesday

  include '_Wild_Pepper';
?>

% cat __intro
...
      # dt - print date and time of event
      #
      function dt($date, $time) {

        print "      <tr><td>&nbsp;</td></tr>\n\n"
            . "      <tr valign=top>\n"
            . "        <td width=100>Date:</td>\n"
            . "        <td>$date</td>\n"
            . "      </tr>\n"
            . "      <tr><td>&nbsp;</td></tr>\n\n"
            . "      <tr valign=top>\n"
            . "        <td width=100>Time:</td>\n"
            . "        <td>$time</td>\n"
            . "      </tr>\n"
            ;
      }

      # dse - get date string for the event
      #
      #  $wn - week number (1-5)
      #  $dn - day  number (0-6)
      #
      function dse($wn, $dn) {

        # Get the current date.

        list(,,,$mday,$mon,$year,,,) = localtime();

        $mon++;         # Use PHP's month numbering.

        # Second iteration handles "happened already".

        for ($try=1; $try<=2; $try++) {

          # Get the day of the week for the 1st ($dw1).

          $fom  = mktime(0, 0, 0, $mon, 1, $year);

          list(,,,,,,$dw1,,) = localtime($fom);

          # Get the date for the event.

          $doe = ($dn+1) + 7 * ($wn-1) - $dw1;

          if ($dw1 > $dn) $doe += 7;

          if ($doe >= $mday) {  # Hasn't happened yet.

            # Return a formatted date string.

            $toe  = mktime(0, 0, 0, $mon, $doe, $year);
            $date = date('l, F j, Y', $toe);
            return $date;
          }

          # Whoops; shift to next month and/or year!

          $mday = 1;


          if (++$mon == 13) { $mon=1; $year++; }
        }

        return "Yow! Couldn't find a reasonable date.";
      }
...
-- 
Technical editing and writing, programming, and web development:
  http://www.cfcl.com/rdm/resume

Contact information: rdm at cfcl.com, +1 650-873-7841


More information about the SanFrancisco-pm mailing list