0

I have this beautiful and versatile calendar script

$giorno = isset($_GET['g']) ? $_GET['g'] : date('Y-m-d');
$day = strtotime($giorno);
$today = date('Y-m-d', strtotime('today'));
$date = mktime(0, 0, 0, date("m", $day), 1, date("Y", $day));
$past = mktime(0, 0, 0, date("m", $date)-1, 1, date("Y", $date));
$future = mktime(0, 0, 0, date("m", $date)+1, 1, date("Y", $date));

$days = array("Domenica", "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato");
$months = array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");

$month = date("m", $date);
$year = date("Y", $date);

$day_offset = 1; //$day_offset % 7;
$start_day = mktime(0, 0, 0, $month, 1, $year); 
$start_day_number = date("w", $start_day);
$days_in_month = date("t", $start_day);

<table id="calendar">
<caption>
    <div class="left"><a href="<?php echo $phpSelf; ?>?g=<?php echo date('d/m/Y', $past); ?>" title="Mese precedente">‹</a></div>
    <div class="right"><a href="<?php echo $phpSelf; ?>?g=<?php echo date('d/m/Y', $future); ?>" title="Mese successivo">›</a></div>
    <div class="center"><?php echo $months[$month-1]; ?> <?php echo $year; ?></div>
</caption>
<tr>
<?php for ($x = 0; $x <= 6; $x++) { ?>
    <th scope="col"><?php echo substr(strtolower($days[($x+$day_offset)%7]), 0, 2); ?></th>
<?php } ?>
</tr>
<tr>
<?php
$blank_days = $start_day_number - $day_offset;
if ($blank_days < 0) { $blank_days = 7 - abs($blank_days); }
for ($x = 0; $x < $blank_days; $x++) {
?>
    <td>&nbsp;</td>
<?php } ?>
<?php for($x = 1; $x <= $days_in_month; $x++) { ?>
    <?php   if (($x + $blank_days-1) % 7 == 0){ ?>
    </tr>
    <tr>
    <?php } ?>
    <?php $day = $year . "-" . $month . "-" . leading($x); ?>
    <td><?php echo $x; ?></td>
<?php } ?>  
<?php while ((($days_in_month + $blank_days) % 7) != 0) { ?>
    <td>&nbsp;</td>
<?php   $days_in_month++;
}
?>
</tr>

It simply loops, given a day, through the entire month and draws a table calendar: so far so good
Now the tricky part: I have a recordset of events like this:

mysql_select_db($database_connArtesicilia, $connArtesicilia);
$query_rsEvents = "SELECT eventi.eve_id, eventi.eve_name, eventi.eve_from, eventi.eve_to FROM eventi WHERE eventi.eve_active";
$rsEvents = mysql_query($query_rsEvents, $connArtesicilia) or die(mysql_error());
$row_rsEvents = mysql_fetch_assoc($rsEvents);
$totalRows_rsEvents = mysql_num_rows($rsEvents);

I'd like to highlight the calendar cells (ie. with <td class="highlight">) corresponding to events, and that would be not so difficult if events were limited to only one day... but some events span through multiple contiguous days (ie. from 5th to 8th december) and I have no idea how to highlight cells corresponding to 5, 6, 7 and 8
Please notice that database fields eve_from and eve_to are ALWAYS filled (if an event occur in only one day, dates are the same) More, some events could overlap (ie. one from 5th to 8th and the other from 7th to 9th december): in an ideal world, I'd need both to simply highlight the cell if one or more events are present (for the small version of the calendar), and to list the event(s) of a cell (for the big version of the calendar)
Have you got some idea on how to achieve this?
Thanks in advance, as usual!

Ivan
  • 2,463
  • 6
  • 39
  • 51
  • [Please, don't use `mysql_*` functions in new code](http://stackoverflow.com/q/12859942). They are no longer maintained and the deprecation process has begun, see the [red box](http://php.net/mysql-connect). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli); [this article](http://php.net/mysqlinfo.api.choosing) will help you decide which. If you choose PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – vascowhite Dec 12 '12 at 16:52

1 Answers1

2

I've actually written a few calendar scripts in the past that dealt with this problem. One thing you might want to to do first is checkout how Google calendars handles this. It can be done purely with some js and css, but your existing PHP code makes it a bit of a kludge to work with.

I can show you my old calendar class and how I approached the problem to help you out.

Here's a link to the code I used.

It's a simple class that uses DateTime objects to create a calendar very similar to yours. You can see how easy it would be to modify that code to include adding some events and highlighting them by simply checking that the event date lies within the given calendar day throughout each iteration.

In fact, here, I took an extra 2 minutes to modify it myself and show you a working example.

So the underlying problem you're having is how you're dealing with the dates. You're suing timestamps and formatted dates and that's making your logic a bit more tangly to work around.

I strongly suggest using PHP's DateTime class. It makes dealing with dates so much easier and abstract. You can see both the working example and code in that link where I've simply added two events to demonstrate how overlapping might be handled.

We have 1 event set from Dec 12th, 2012 through Dec 16th, 2012, which is highlighted in green and another event set from Dec 15th, 2012 through Dec 21st, 2012 which is highlighted in red. Now, because the calendar is tiny I simply use non-breaking-spaced divs with a set background color in each cell. It's not pretty but it gives you some idea to get you started.

Feel free to use any of that code and I hope that helps. Cheers!

P.S: I would strongly encourage you to switch away from ext/mysql mysql_* functions to interface with your mysql database in PHP and consider moving to the newer APIs such as PDO or MySQLi as ext/mysql is now deprecated and will be removed in future versions of PHP.

Sherif
  • 11,786
  • 3
  • 32
  • 57