kamal213 Posted January 12, 2012 Share Posted January 12, 2012 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'"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/254884-event-start-and-end-date-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 12, 2012 Share Posted January 12, 2012 See this - http://www.phpfreaks.com/forums/index.php?topic=350918.msg1656495#msg1656495 Quote Link to comment https://forums.phpfreaks.com/topic/254884-event-start-and-end-date-help/#findComment-1306882 Share on other sites More sharing options...
kamal213 Posted January 12, 2012 Author Share Posted January 12, 2012 Dont you mean I have to change the format to YYYY-mm-dd? tried that and it didnt work Quote Link to comment https://forums.phpfreaks.com/topic/254884-event-start-and-end-date-help/#findComment-1306892 Share on other sites More sharing options...
kamal213 Posted January 12, 2012 Author Share Posted January 12, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254884-event-start-and-end-date-help/#findComment-1306931 Share on other sites More sharing options...
PFMaBiSmAd Posted January 12, 2012 Share Posted January 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254884-event-start-and-end-date-help/#findComment-1306951 Share on other sites More sharing options...
kamal213 Posted January 12, 2012 Author Share Posted January 12, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254884-event-start-and-end-date-help/#findComment-1306967 Share on other sites More sharing options...
kamal213 Posted January 13, 2012 Author Share Posted January 13, 2012 I'm starting to get it a bit..so basically rather than using php to extract the dates mysql can extract them for me so long as its in the right format? Quote Link to comment https://forums.phpfreaks.com/topic/254884-event-start-and-end-date-help/#findComment-1307153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.