Jump to content

Event Start and End Date Help


kamal213

Recommended Posts

Hi Guys,

 

I have an events calender which if theres an events it echos a color if blue on the day. I would like to display the start and end dates on the calender in the same way. Example if and events starts 01/01/2012 and ends 03/01/2012.

 

I'm having problems echoing a blue color from 01/01/2012 to 03/01/2012 to indicate the start and end date of the event.

 

The code belows show how I tried to solve the problem, it works but it thinks the event start from the 1st and end on the 3rd of every month which is wrong.

 

Please show me how I can improve this code or do different to make it work.

Thanks

    $todaysDate = date("d/m/Y");
    $dateToCompare = $daystring . '/' . $monthstring . '/' . $year;
    echo "<td align='center' ";
    if($todaysDate == $dateToCompare){
    echo "style='class:red'";
    }else{
    $sqlcount = mysql_query("select * from event where '".$dateToCompare."' >=start_date and '".$dateToCompare."' <=end_date");
    $customerCount = mysql_num_rows($sqlcount); // count the output amount
    while($row = mysql_fetch_assoc($sqlcount)) { 
    $start_date = $row['start_date'];
    $end_day = $row['end_day'];
    if($customerCount >= 1){  
    echo "style='class:blue'";
    } 
    }
    }

Link to comment
Share on other sites

 

I have made the following amendment:

    $sqlcount = mysql_query("select start_date, end_date from event where '".$dateToCompare."'");
    $customerCount = mysql_num_rows($sqlcount); // count the output amount
    while($row = mysql_fetch_assoc($sqlcount)) { 
    $start_date = $row['start_date'];
    $end_date = $row['end_date'];
    						
    if($start_date <=$dateToCompare && $dateToCompare<=$end_date){ 
    						
    echo "style='class:blue'";
    } 
    }

The problem now is when I click on the next month/previous button I hightlight from the 1st to 3rd of every month indicating an event even though the event is from the 01/01/2012-03/01/2012.

 

Could you help with more ideas on what am doing wrong.

 

Thanks

Link to comment
Share on other sites

hightlight from the 1st to 3rd of every month

 

That's because you cannot do greater-than/less-than comparisons on dates unless they are all in the proper format. Someone has already stated that and provided links to pages that explain why this is so and even to some code you could examine to see how you would loop over the dates for a selected month and test if your data has an event for any day in that month.

 

The link I posted that eventually takes you to the post that starts - 'A slight bit of number/string comparison theory' is to some information that you learned in about the 4th grade when comparing integer numbers, but applied to a formatted date string.

 

Without knowing what your database values are and what the value is in your $dateToCompare variable, it is not possible to help you with the latest symptom. Also, your WHERE clause in your query is just a TRUE value and is retrieving all the data in your table, that you are then trying to filter/match in your php code. You would never want to do that in a real application.

Link to comment
Share on other sites

Thanks for the response, I will have a look at the post again

 

But here is more information about my event calender

 

$row['start_date'] = the row which holds event start date is a (varchar 255);   

$row['end_date'] = the row which holds event end date is a (varchar 255);

All date are in d/m/y format as my users requested.

 

This represents the event date string

$dateToCompare = $daystring . '/' . $monthstring . '/' . $year;

 

This is the full event calender in full

<?php 
			  echo "<tr>";
			  //looping from 1 to the number of days in the month
			  for($i = 1; $i < $numDays+1; $i++, $counter++){
			  //males a timestamp for each day in the loop
			  			$timeStamp = strtotime("$year-$month-$i");
						//makes a check if it is 'day 1'
						if($i == 1){
						//gets which day 'day 1' fall on
						$firstDay = date("w", $timeStamp);
						//makes a loop and makes a blank cell if it's not the first day
						for($j = 0; $j < $firstDay; $j++, $counter++){
						//blank space
						echo "<td> </td>";
						}
					}
					if($counter % 7 == 0){
					echo "</tr><tr>";
					}
					$monthstring = $month;
					$monthlength = strlen($monthstring);
					//day string get from $i loop
					$daystring = $i;
					$daylength = strlen($daystring);
					if($monthlength <= 1){
						$monthstring = "0".$monthstring;
					}
					if($daylength <= 1){
						$daystring = "0".$daystring;
					}

					$todaysDate = date("d/m/Y");
		            $dateToCompare = $daystring . '/' . $monthstring . '/' . $year;
					echo "<td align='center' ";
					if($todaysDate == $dateToCompare){
					echo "class='today'";
					}else{
					$sqlcount = mysql_query("select b_date, b_day from csj_test where '".$dateToCompare."'");
					$customerCount = mysql_num_rows($sqlcount); // count the output amount
					while($row = mysql_fetch_assoc($sqlcount)) { 
					$b_date = $row['b_date'];
					$b_day = $row['b_day'];

					if($b_date <=$dateToCompare && $dateToCompare<=$b_day){ 

					echo "class='event'";
					} 
					}
					}
					echo "><strong><p align='center'><a href='system_diary.php?&day=" . $daystring . "&month=" . $monthstring . "&year=" . $year . "&v=true'>".$i."</a></p></strong></td>";	
			  }
			  echo "</tr>";
			  ?>

 

Please let me know if this helps

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.