Jump to content

PHP help specifying date then echoing HTML?


Izzy-B
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi.  I'm trying to achieve a calendar that only uses PHP and CSS.  I registered today to ask for help (because I'm not a coder; I'm just another clueless musician).   :)

 

My goal is to just skip the database, altogether.  I've already, so far, skipped the Javascript, as well. I have an HTML non-functioning mockup HERE that's a bit "off" cosmetically, but I'm just using it as an example.  It has a made-up "event" on August 16.  I'm just using a little "tooltip" CSS.  (I have no idea how it works in IE, it's just me and my MacBook here, but still... you get the idea of the function.) Or I might use THIS to get it to work for IOS, but that needs a little aesthetic work.  Not tinkering with all that today.

Now... my PHP created calendar, that I will actually be using, is HERE.  All the structure of the calendar is echoed in PHP so there is no actual HTML hard coding of table cells.  In fact, the only HTML within the body is <body></body>  So, I can't very well just pick a date and type in some tags and be off.

My question is this (and because I don't really know correct PHP jargon to formulate a question, I'm not being successful at Googling): Is it possible to use PHP to isolate a specific date, say, in this case, August 16 2014, and then, based upon that date existing, echo what I have hard-coded into the mockup example: 

<div class="has-tooltip">

   Event

    <span class="tooltip">Concert In The Park<br />

    123 City Park Drive<br />

    AnyTown, STATE 00000<br />

    <a href="http://example.com" target="_blank">More Info</a><br />

    or call 555.123.4567<br /></span>

</div>

I realize that this means that there's always a great chance of error, but this wouldn't be a calendar with any other users inserting dates; it's nothing more than an occasional event and then it's over, and I would be the one posting the events, so I'd be able to test it before publishing it.

Possible?  And if so, would you advise as how to get PHP to isolate a particular date and then echo the HTML and, of course, have it reside in the date-appropriate table cell?  (I've managed to echo HTML to build the calendar structure, but I've never created an echo in conjunction with calling for a specific date.  I'd appreciate any help and thanks so much.

PHP:

<?php
    $currMonth = isset($_GET['month']) ? $_GET['month']  : date('n');
    $currYear  = isset($_GET['year'])  ? $_GET['year']   : date('Y');
    $today = (($currYear == date('Y')) && ($currMonth == date('n'))) ? date('j') : 0;

    $prevMonth = $currMonth==1 ? 12 : $currMonth-1;
    $nextMonth = $currMonth==12?  1 : $currMonth+1;
    $prevYear = $currMonth==1 ? $currYear-1 : $currYear;
    $nextYear = $currMonth==12? $currYear+1 : $currYear;


    $day1 = mktime(0,0,0,$currMonth,1,$currYear);
    $dim = date('t', $day1);
    $dayN = mktime(0,0,0,$currMonth,$dim,$currYear);
    $dow1 = (date('w',$day1)+0) % 7;
    $dowN = (date('w',$dayN)+0) % 7;
    $calHead = date('F Y',$day1);
    echo <<<EOT
        <div class="calwrapper">
        <div class="caltitle"><h1>Calendar</h1></div>
        <div class="container">
        <div class="fnl first"></div>
        <div class="adjust"></div>
         <div class="fnl last"></div>
         </div>
         <div class="caldisplay">
        <table cellspacing="0">
        <tr>
          <td class="hd"><a class="cal_button" href="$_SERVER[php_SELF]?year=$prevYear&month=$prevMonth"> Prev </a></td>
          <td colspan="5" class="adjust">$calHead</td>
          <td class="hd"><a class="cal_button" href="$_SERVER[php_SELF]?year=$nextYear&month=$nextMonth"> Next </a></td>
        </tr>
        <tr>
          <th class="we">Sun</th>
          <th class="wd">Mon</th>
          <th class="wd">Tue</th>
          <th class="wd">Wed</th>
          <th class="wd">Thu</th>
          <th class="wd">Fri</th>
          <th class="we">Sat</th>
         </tr>
        <tr>
EOT;

    for ($d=0;$d<$dow1;$d++) echo "<td class=\"hd\"> </td>";
    $c = $dow1;
    for ($d=1; $d<=$dim; $d++, $c++) {
        if ($c%7==0) echo "</tr><tr>";
        $cl = ($c%7==5) || ($c%7==6) ? 'we' : 'wd';
        $st = ($d == $today) ? "style='padding: 0px;'" : '';
        echo "<td class=\"$cl\" $st>\n";
        echo "$d" ;
        echo "</td>\n";
    }
    while ($c++ % 7 != 0) echo '<td class=\"hd\"> </td>';
    echo "</tr></table>\n";
    echo '</div></div>';
   
    

?>

 

 
Link to comment
Share on other sites

  • Solution

So you want to add entries to the calendar that is assigned to a specific date. For example you have an entry for August 16 2014 that you want to list for that date

 

You could stored all entries for the calendar in an formatted like so  (later to be populated by results from your database or flat file [csv or xml etc]), example

// calander entries. Use the date as the key (in YYYY/MM/DD format)
$entries = array(
	'2014/8/16' => array(
        				'Text to be shown on August 16 2014',
					),
	'2014/8/24' => array(
        				'First entry for August 24 2014',
        				'Second Entry for August 24 2014',
					),
	'2014/12/25' => array(
        				'Xmas day!',
					),
)

Here I have used the dates (in YYYY/MM/DD format) as an array key, all events for that date will be stored in an sub array, in case a date has more than one event.

 

The following code is responsible for listing the days in the month

    for ($d=1; $d<=$dim; $d++, $c++) {

        if ($c%7==0) echo "</tr><tr>";

        $cl = ($c%7==5) || ($c%7==6) ? 'we' : 'wd';

        $st = ($d == $today) ? "style='padding: 0px;'" : '';

        echo "<td class=\"$cl\" $st>\n";

        echo "$d" ;

        echo "</td>\n";

    }

You'd add  an if statement after   echo "$d" ;  to check if the current date being outputted for the calender exists in the $entries array, example

        // construct the date, this will be used to check to if the key exists in the $entries array
        $dateKey = "$currYear/$currMonth/$d";
        // check if the key exists in the $entries array
        if(array_key_exists($dateKey, $entries)) {
            // for each event, list it in a seperate tool tip
            foreach($entries[$dateKey] as $entry) {
                echo '<div class="has-tooltip">
           Event
            <span class="tooltip">'.$entry.'</span>
        </div>';
            }
        }
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.