I am in the process of writing a "Press Release" CGI application in Perl for a client's website.

The database is of a high volume with new press-releases being issued on a daily basis (Mon - Fri). As such, they have requested that the script offer a means by which the user can select a time frame and press releases issued within that time frame are displayed.

I have dealt with "Today's", "This Month's", "Most Recent" (displayed 20 to a page) and all seem to be working fine.

My client also wants a "This Week's" option - but, it is to be based on a calendar working week, not "display anything published within the last seven days" (which would have been easy). That is, if today is Monday, display only those articles published today, if Tuesday - display today's and yesterday's, if Wednesday - display today's, Monday's and Tuesday's... etc. If today is Sunday, display everything published between the Monday and Friday immediately prior to today.

My solution to this can be described as follows:

If today is Monday then display only those documents published on a Monday with an epoch time less than 604,800 seconds (one day) difference from the current epoch time;
If today is Tuesday then display only those documents published on either a Monday or a Tuesday with an epoch time less than (604,800 * 2) difference from the current epoch time; and so on.

Notes pertaining to the code segment over the page:

  • "$this_day" is the current day in "human" form based on the system clock (i.e. "Monday", "Tuesday"...);
  • "$time" is the current system time in epoch seconds (from Perl's "time" function);
  • "$this_record2" contains a time-stamp that is the epoch time of the record's creation;
  • "$record_day" contains the day in "human" form on which the record was created;
  • A hash called "display_index" is being populated with the reference key and creation time (in epoch seconds) of each matching record.

    This whole chunk of code is being executed recursively within a while loop ("while (($key, $value) = each %index) {") as the program evaluates each record in the database (a tied hash ("%index") to a GDBM database).

    if ($sort_method eq "week") { $record_day = $this_record[0]; $record_day =~ s/(\w*?) .*/$1/so; if ($this_day eq "Monday") { if (($time - $this_record[2]) <= 604800) and ($record_day eq $this_day +)) { $display_index{$key} = $this_record[2]; } } if ($this_day eq "Tuesday") { if (($time - $this_record[2]) <= (604800 * 2)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Wednesday") { if (($time - $this_record[2]) <= (604800 * 3)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday") or ($recor +d_day eq "Wednesday")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Thursday") { if (($time - $this_record[2]) <= (604800 * 4)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday") or ($recor +d_day eq "Wednesday") or ($record_day eq "Thursday")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Friday") { if (($time - $this_record[2]) <= (604800 * 5)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday") or ($recor +d_day eq "Wednesday") or ($record_day eq "Thursday") or ($record_day eq "Fri +day")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Saturday") { if (($time - $this_record[2]) <= (604800 * 6)) { if (($record_day eq "Monday") or ($record_day eq "Tuesday") or ($recor +d_day eq "Wednesday") or ($record_day eq "Thursday") or ($record_day eq "Fri +day") or ($record_day eq "Saturday")) { $display_index{$key} = $this_record[2]; } } } if ($this_day eq "Sunday") { if (($time - $this_record[2]) <= (604800 * 7)) { $display_index{$key} = $this_record[2]; } } }

     

    In theory, there is no difference between theory and practise.  But in practise, there is.
     
    Jonathan M. Hollin
    Digital-Word.com

    In reply to There's More Than One Way To Do It by DarkBlue

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.