My collection of things.

jKafer on the Net

May 25th, 2011 at 7:17 pm

php Multidimensional arrays

Lately I have been working on some small snippets of code to automate tasks I do often, yet not often. I do them so rarely that I forget how I did it the last time. But yet, I have to do it every year – well around every holiday at least.

I take care of an international webpage for a customer service department. I need to post the holiday shutdown schedule when we come close to a holiday. Luckily there are only 6 of them. So to automate the message I finally sat down and wrote this snippet to automatically display the message about a closing day 14 days before the holiday – it’s more reliable that I am to update itself.

Is it the best way? Maybe, maybe not.
Is it the only way? Absolutely not.
But this works for what I need. I update it once a year when our company releases it’s shutdown schedule.

I am using a multidimensional array, I’ve set the key for the 2nd level.

Here it is:

<?
$tToday = date("Y-m-d");
//array structure:   array( startdate, enddate, displaydate, holiday);
$wSched = array(0 => array( '2012-1-1', '2012-1-2', '2011-12-18', 'New Years'),
    1 => array( '2011-5-30','2011-5-30', '2011-5-16', 'Memorial Day'),
    2 => array( '2011-7-4', '2011-7-4', '2011-6-20', 'July 4th'),
    3 => array( '2011-9-5', '2011-9-5', '2011-8-22', 'Labor Day'),
    4 => array( '2011-11-24', '2011-11-28', '2011-11-10', 'Thanksgiving'),
    5 => array( '2011-12-26', '2011-12-27', '2011-12-12', 'Christmas'));

$tMsg = "<h2>ATTENTION</h2>";
$tMsgL = strlen($tMsg);
asort($wSched);
 for ($col=0; $col <6; $col++)
  { 
   if(strtotime($tToday) >= strtotime($wSched[$col][2]) && strtotime($tToday) <=
strtotime($wSched[$col][1]) )
    {          
      $tMsg.= "<p>Mate will be closed on <br />".date("D, F j, Y",
strtotime($wSched[$col][0]));
      if ( strtotime($wSched[$col][1]) != strtotime($wSched[$col][0]) )  
         {
           $tMsg .= " thru ".date("D, F j, Y", strtotime($wSched[$col][1]));
         }
      $tMsg .= "<br /> in observation of ".$wSched[$col][3]." Holiday</p>";
    };  

  }; //end for

if (strlen($tMsg)> $tMsgL)
{
   print $tMsg;
};
?>

-

Comments are closed.

  • Pages

  • Latest Pins on Pinterest

    • Follow Me on Pinterest
  • Quilting Projects I Want to Make

    • Leading Edge Quilt Pattern PDF Download - Etsy

    • Homecoming Quilt- My Peppered Cotton version & tester quilts! – Lo & Behold Stitchery

    • Homecoming Quilt- My Peppered Cotton version & tester quilts! – Lo & Behold Stitchery

    • On the Edge is a quick and fun modern quilt pattern with a lot of custom design possibilities! This PDF pattern includes directions for making a twin-size quilt (72 x 96), a throw-size quilt (56 x 64), a baby size (38.5 x 44), and a pillow/mini quilt (18 x 18). See the Instagram hashtag

    • Adelita Quilt Pattern digital Download - Etsy

    • Follow Me on Pinterest