Availability Calendar not showing selected dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Availability Calendar not showing selected dates

Post by jonnyfortis »

I am using an availability scrip from http://www.phpjabbers.com/free-availabi ... ar-script/ . what seems to be happening if on the actual month (that it is when i add a new date it highlights all that dates added ok but addng earlier or later dates is intermittent on what it is displaying for example i could add 23,24,25 september and they are displayed ok but then go to add the 3rd september and it wont show. (even though all the dates are stored in the DB they are not showing in the calender. it very intermittent on dates so hard to pin down the issue or what to show here at the moment.

dates are stored in 2014-07-27 format (this is 27th Aug 2014)

thanks in advance
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Availability Calendar not showing selected dates

Post by Celauran »

Please post the code in question and maybe some of the rows from the DB; a few that work and at least one that doesn't. Telling us that some black box code gives inconsistent results gives us nothing to work with.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Availability Calendar not showing selected dates

Post by jonnyfortis »

Celauran wrote:Please post the code in question and maybe some of the rows from the DB; a few that work and at least one that doesn't. Telling us that some black box code gives inconsistent results gives us nothing to work with.
here is the calender.php

Code: Select all

<?php

error_reporting(0);
include("config.php");

/// get current month and year and store them in $cMonth and $cYear variables
(intval($_REQUEST["month"])>0) ? $cMonth = $_REQUEST["month"] : $cMonth = date("n");
(intval($_REQUEST["year"])>0) ? $cYear = $_REQUEST["year"] : $cYear = date("Y");

if ($cMonth<10) $cMonth = '0'.$cMonth;

// generate an array with all unavailable dates
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE `date` LIKE '".$cYear."-".$cMonth."-%'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
	$unavailable[] = $row["date"];
}

// calculate next and prev month and year used for next / prev month navigation links and store them in respective variables
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = intval($cMonth)-1;
$next_month = intval($cMonth)+1;

// if current month is Decembe or January month navigation links have to be updated to point to next / prev years
if ($cMonth == 12 ) {
	$next_month = 1;
	$next_year = $cYear + 1;
} elseif ($cMonth == 1 ) {
	$prev_month = 12;
	$prev_year = $cYear - 1;
}
?>

Code: Select all

<table width="800">
  <tr>
      <td class="mNav"><a href="javascript:LoadMonth('<?php echo $prev_month; ?>', '<?php echo $prev_year; ?>')"><<</a></td>
      <td colspan="5" class="cMonth"><?php echo date("F, Y",strtotime($cYear."-".$cMonth."-01")); ?></td>
      <td class="mNav"><a href="javascript:LoadMonth('<?php echo $next_month; ?>', '<?php echo $next_year; ?>')">>></a></td>
  </tr>
  <tr>
      <td class="wDays">M</td>
      <td class="wDays">T</td>
      <td class="wDays">W</td>
      <td class="wDays">T</td>
      <td class="wDays">F</td>
      <td class="wDays">S</td>
      <td class="wDays">S</td>
  </tr>
<?php 
$first_day_timestamp = mktime(0,0,0,$cMonth,1,$cYear); // time stamp for first day of the month used to calculate 
$maxday = date("t",$first_day_timestamp); // number of days in current month
$thismonth = getdate($first_day_timestamp); // find out which day of the week the first date of the month is
$startday = $thismonth['wday'] ; // 0 is for Sunday and as we want week to start on Mon we subtract 1
if (!$thismonth['wday']) $startday = 7;
for ($i=1; $i<($maxday+$startday); $i++) {
	
	if (($i % 7) == 1 ) echo "<tr>";
	
	if ($i < $startday) { echo "<td>&nbsp;</td>"; continue; };
	
	$current_day = $i - $startday + 1;
	
	(in_array($cYear."-".$cMonth."-".$current_day,$unavailable)) ? $css='booked' : $css='available'; // set css class name based on date availability
	
	echo "<td class='".$css."'>". $current_day . "</td>";
	
	if (($i % 7) == 0 ) echo "</tr>";
}
?> 
</table>
the database

showing on the calender

date
2014-09-26
2014-09-25
2014-09-24

displayed in the db but not showing
2014-09-03
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Availability Calendar not showing selected dates

Post by Celauran »

It's checking 2014-09-3 against 2014-09-03, which isn't a match. You need to pad the dates.

Change this:

Code: Select all

$current_day = $i - $startday + 1;
to this:

Code: Select all

$current_day = str_pad(($i - $startday + 1), 2, '0', STR_PAD_LEFT);
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Availability Calendar not showing selected dates

Post by jonnyfortis »

Celauran wrote:It's checking 2014-09-3 against 2014-09-03, which isn't a match. You need to pad the dates.

Change this:

Code: Select all

$current_day = $i - $startday + 1;
to this:

Code: Select all

$current_day = str_pad(($i - $startday + 1), 2, '0', STR_PAD_LEFT);
yes that done it, thanks, any dates that are single numbers i.e 1,2,3,4 etc.. would cause the error?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Availability Calendar not showing selected dates

Post by Celauran »

That's right. Because of how it iterates over the dates, you'll need to pad 1-9 in order to get a match.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: Availability Calendar not showing selected dates

Post by jonnyfortis »

Celauran wrote:That's right. Because of how it iterates over the dates, you'll need to pad 1-9 in order to get a match.
oh ok, thanks for that
Post Reply