marcusfaye87 Posted May 5, 2008 Share Posted May 5, 2008 I built this and it works fine, my question is simple though. When I finished building it I thought to myself: "Couldn't this be done easier?". So that's my question to you. I haven't checked other resources because I didn't know where to begin. Thanks in advance, Marcus Faye Functions used from db_functions.php function select_data ($table, $order, $limit) { $table = mysql_real_escape_string ($table); $order = mysql_real_escape_string ($order); $limit = mysql_real_escape_string ($limit); $query = "SELECT * FROM `$table` ORDER BY `$order` DESC LIMIT $limit"; $select = mysql_query ($query); return $select; } function split_date ($dateTime) { $splitTime ['year'] = substr ($dateTime,0,4); $splitTime ['month'] = substr ($dateTime,5,2); $splitTime ['day'] = substr ($dateTime,8,2); $dateTimeLength = strlen ($dateTime); if ($dateTimeLength == 19) { $splitTime ['hour'] = substr ($dateTime,11,2); $splitTime ['minute'] = substr ($dateTime,14,2); $splitTime ['second'] = substr ($dateTime,17,2); } return $splitTime; } events.php <table width=550 cellpadding=0 cellspacing=0> <tr> <td id=menu>Events calander</td> </tr> <?php // Check if the events have passed, are upcomming, or are today // Get the events data $query = select_data ("events", "id", "20"); // Loop the events rows while ($eventData = mysql_fetch_array ($query)) { // Split the event date into an array $eventDate = split_date ($eventData['date']); // Split the current date (global DATETIME variable) into an array $currentDate = split_date (DATETIME); // Copy the array to a temporary variable $temp = $eventData; if ($eventDate['year'] > $currentDate['year']) { // Event is upcomming $eventsArrayUpcomming[] = $eventData; } elseif ($eventDate['year'] == $currentDate['year']) { if ($eventDate['month'] > $currentDate['month']) { // Event is upcomming $eventsArrayUpcomming[] = $eventData; } elseif ($eventDate['month'] == $currentDate['month']) { if ($eventDate['day'] > $currentDate['day']) { // Event is upcomming $eventsArrayUpcomming[] = $eventData; } elseif (if $eventDate['day'] == $currentDate['day']) { // Event is today $eventsArrayToday[] = $eventData; } elseif (if ($eventDate['day'] < $currentDate['day']) { // Event has passed $eventsArrayPassed[] = $eventData; } } elseif ($eventDate['month'] < $currentDate['month']) { // Event has passed $eventsArrayPassed[] = $eventData; } } elseif ($eventDate['year'] < $currentDate['year']) { // Event has passed $eventsArrayPassed[] = $eventData; } } // Output events TODAY // Echo title echo " <tr> <td id=title>Events today</td> </tr> "; // Count the array for events today $count = count ($eventsArrayToday); // Loop through the array for ($i = 0; $i < $count; $i++) { // Output echo " <tr> <td id=post>" . $eventsArrayToday[$i]['event'] . "</td> </tr> "; } // Output events UPCOMMING // Echo title echo " <tr> <td id=title>Upcomming events</td> </tr> "; // Count the array for events upcomming $count = count ($eventsArrayUpcomming); // Loop through the array for ($i = 0; $i < $count; $i++) { // Output echo " <tr> <td id=post>" . $eventsArrayUpcomming[$i]['event'] . "</td> </tr> "; } // Output events PASSED // Echo title echo " <tr> <td id=title>Passed events</td> </tr> "; // Count the array for events passed $count = count ($eventsArrayPassed); // Loop through the array for ($i = 0; $i < $count; $i++) { // Output echo " <tr> <td id=post>" . $eventsArrayPassed[$i]['event'] . "</td> </tr> "; } ?> <td> </td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
Michan Posted May 5, 2008 Share Posted May 5, 2008 For this chunk: if ($eventDate['year'] > $currentDate['year']) { // Event is upcomming $eventsArrayUpcomming[] = $eventData; } elseif ($eventDate['year'] == $currentDate['year']) { if ($eventDate['month'] > $currentDate['month']) { // Event is upcomming $eventsArrayUpcomming[] = $eventData; } elseif ($eventDate['month'] == $currentDate['month']) { if ($eventDate['day'] > $currentDate['day']) { // Event is upcomming $eventsArrayUpcomming[] = $eventData; } elseif (if $eventDate['day'] == $currentDate['day']) { // Event is today $eventsArrayToday[] = $eventData; } elseif (if ($eventDate['day'] < $currentDate['day']) { // Event has passed $eventsArrayPassed[] = $eventData; } } elseif ($eventDate['month'] < $currentDate['month']) { // Event has passed $eventsArrayPassed[] = $eventData; } } elseif ($eventDate['year'] < $currentDate['year']) { // Event has passed $eventsArrayPassed[] = $eventData; } You could simply convert the event year, month, and day to a timestamp and match it with time(). From what I see, you don't need to convert it to the year, month, and day. $eventDate['date'] = mktime(0, 0, 0, $eventDate['month'], $eventDate['day'], $eventDate['year']) if (date('jmY', $eventDate['date']) > date('jmY', time())) { // Event is upcomming $eventsArrayUpcomming[] = $eventData; } elseif (date('jmY', $eventDate['date']) == date('jmY', time())) { // Event is today $eventsArrayToday[] = $eventData; } else { // Event has passed $eventsArrayPassed[] = $eventData; } Quote Link to comment Share on other sites More sharing options...
Michan Posted May 5, 2008 Share Posted May 5, 2008 Edit: Double post Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted May 5, 2008 Author Share Posted May 5, 2008 well tbh, the date I get from the database is a DATE timestamp in mysql already. I insert the date like this: date ('Y-m-d') So in the database "date" field it looks like this. 2008-04-05 So do I still need to use the mktime command? I'm not so good with working with dates is why. I have no Idea if you can just subtract one date from another. Edit: nevermind, I gues you can only do math with timestamps, not with dates themselves Thanks for the help on that part. Any other changes I can make to make the script more efficient and smaller? Quote Link to comment 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.