
gkornbluth
Veteran
Mar 7, 2010, 5:06 PM
Post #4 of 18
(7410 views)
Shortcut
|
|
Re: [rez] displaying tonights performing artist
[In reply to]
|
Can't Post
|
|
Hi Rez, As Dave Edis says, "Date math makes my head hurt." Well, it hurts mine too. That said, here are 2 (long) recipes that might be modified to reach your goal. They're taken out of my CMSB Cookbook http://www.thecmsbcookbook.com Good Luck, Jerry Kornbluth COMPARING DATES When combined with the “if” statement, this extremely useful concept can be used to show or hide fields based on a particular date, automatically archive records after a specified time period and perform many other functions. Here’s the basic idea of how to compare a date field to today’s date on a list page. This example uses a date field called “opening_reception” in a multi record editor called “exhibitions”. The goal was to automatically hide the opening reception date after the date had passed.
<!– First you’ll need a “foreach” statement to display each record. --> <?php foreach ($exhibitionsRecords as $record): ?> Since dates are normally expressed as months, days and years are hard to compare mathematically, the dates are converted to the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) Note that dates are referenced to local time as set in the CMSB “General Settings” information and not server time. Dave suggested to first define a few variables and to use simple, descriptive names so that the process is easier to follow. Then set some rules for the comparisons. You can use mathematical operators like <, >, <= or >= between values to compare them in different ways.
<?php $eventUnixTime = strtotime( $record['reception_date'] ); // seconds since 1970 $eventDateString = date("l, F jS", $eventUnixTime); // example format: Monday, June 1st $currentUnixTime = time(); $currentDateString = date("l, F jS", $currentUnixTime); $isEventToday = ($eventDateString == $currentDateString); // first comparison $isEventOver = !$isEventToday && ($eventUnixTime < $currentUnixTime); // second comparison $isFutureEvent = !$isEventOver && !$isEventToday; // third comparison ?> Then it becomes a simple matter of comparing the variables
<?php if ($isFutureEvent): ?> Opening Reception: <?php echo $eventDateString ?> <?php elseif ($isEventToday): ?> The Opening reception is today. Don't miss it!! <?php else: ?> Sorry, you missed the Opening Reception. <?php endif; ?> <!– and don’t forget the endforeach statement to close your loop.–> <?php endforeach ?> Or, Let’s say you want to group a set of meetings by current, future and past dates You could use:
<?php foreach ($general_meetingsRecords as $record): ?> <?php $eventUnixTime = strtotime( $record['date'] ); // seconds since 1970 $eventDateString = date("l, F jS", $eventUnixTime); // example format: Monday, June 1st $currentUnixTime = time(); $currentDateString = date("l, F jS", $currentUnixTime); $isEventToday = ($eventDateString == $currentDateString); $isEventOver = !$isEventToday && ($eventUnixTime < $currentUnixTime); $isFutureEvent = !$isEventOver && !$isEventToday; ?> <?php if ($isEventToday): ?> <p> <div align="center"class="heading-text-bold">TONIGHT'S MEETING:</div> </p> <br /> <span class="body-text-bold"><?php echo date("D, M jS, Y g:i a", strtotime($record['date'])) ?> </span> <br /> <div align="left" class="body-text"><?php echo $record['content'] ?></div> <hr align="left" color="#A29DB2" width="100" /><br /> <?php endif; ?> <?php endforeach ?> </div> <p> <div align="center"class="heading-text-bold">UPCOMING MEETINGS:</div> </p> <?php foreach ($general_meetingsRecords as $record): ?> <?php $eventUnixTime = strtotime( $record['date'] ); // seconds since 1970 $eventDateString = date("l, F jS", $eventUnixTime); // example format: Monday, June 1st currentUnixTime = time(); $currentDateString = date("l, F jS", $currentUnixTime); $isEventToday = ($eventDateString == $currentDateString); $isEventOver = !$isEventToday && ($eventUnixTime < $currentUnixTime); $isFutureEvent = !$isEventOver && !$isEventToday; ?> <?php if ($isFutureEvent): ?> <br /> <span class="body-text-bold"><?php echo date("D, M jS, Y g:i a", strtotime($record['date'])) ?></span> <br /> <div align="left" class="body-text"> <?php echo $record['content'] ?></div><hr align="left" color="#A29DB2" width="100" /> <?php endif; ?> <?php endforeach ?> <br /> <p><div align="center" class="heading-text-bold">PAST MEETINGS:</div></p> <?php foreach ($general_meetingsRecords as $record): ?> <?php $eventUnixTime = strtotime( $record['date'] ); // seconds since 1970 $eventDateString = date("l, F jS", $eventUnixTime); // example format: Monday, June 1st $currentUnixTime = time(); $currentDateString = date("l, F jS", $currentUnixTime); $isEventToday = ($eventDateString == $currentDateString); $isEventOver = !$isEventToday && ($eventUnixTime < $currentUnixTime); $isFutureEvent = !$isEventOver && !$isEventToday; ?> <?php if ($isEventOver): ?> <br /> <span class="body-text-bold"><?php echo date("D, M jS, Y g:i a", strtotime($record['date'])) ?></span> <br /> <div align="left" class="body-text"><?php echo $record['content'] ?></div><hr align="left" color="#A29DB2" width="100" /> <?php endif; ?> <?php endforeach ?> </div> Note that you have to redefine the variables for each "foreach" loop If you want to compare other field values, just define more variables and compare those as well. If you want to test for the values of the variable that you're using. Try something like this in your “foreach” loop:
<ul> <li>Event date = <?php echo $eventDateString ?></li> <li>Current date = <?php echo $currentDateString ?></li> <li>$isEventToday = <?php echo $isEventToday ? "true" : "false" ?></li> <li>$isEventOver = <?php echo $isEventOver ? "true" : "false" ?></li> <li>$isFutureEvent = <?php echo $isFutureEvent ? "true" : "false" ?></li> <li>$CurrentUnixTime extended = <?php echo date("l jS \of F Y h:i:s A", $currentUnixTime); ?></li> </ul> AND THE SECOND ONE LISTING RECORDS WITHIN A DATE RANGE My client wanted to list all of the events on their list page, but wanted to make a special list at the top of the page for those events with receptions that were going to occur within the upcoming week. Thanks to Dave Edis of Interactive Tools (again) who freely admits that date math makes his head hurt, we came up with the following. First I created a date field in my ‘date_compare_test’ multi record editor called ‘reception_date’. Then came the task of creating a list viewer that would list only the events I wanted in the appropriate groups. Dave came up with the idea of putting 2 viewers on my list viewer page and using 2 different ‘where’ statements in the get records calls to separate the content that could show in each area. Here's the 'where' statement that we used to show reception_ dates for the upcoming week, as you suggested.
'where' => '(NOW() + INTERVAL 7 DAY) >= reception_date AND reception_date >= TIMESTAMP(CURDATE(), "00:00:00")', And here's the 'where' statement that we used to show all the other events (and leave out those already listed) 'where' => 'reception_date <= TIMESTAMP(NOW()) OR reception_date >= TIMESTAMP(NOW()+ INTERVAL 7 DAY)', So here’s how the complete viewer code looked.
<!– Start of first list viewer code–> <?php header('Content-type: text/html; charset=utf-8'); ?> <?php /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */ require_once "/hsphere/local/home/apbcweb/artistsofpalmbeachcounty.org/cmsAdmin/lib/viewer_functions.php"; list($date_compare_testRecords, $date_compare_testMetaData) = getRecords(array( 'tableName' => 'date_compare_test', 'where' => '(NOW() + INTERVAL 7 DAY) >= reception_date AND reception_date >= TIMESTAMP(CURDATE(), "00:00:00")', )); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <hr /> EVENTS WITH RECEPTIONS TODAY THROUGH 7 DAYS IN THE FUTURE<br /><br /> <?php foreach ($date_compare_testRecords as $record): ?> Title: <?php echo $record['title'] ?><br/> The Reception Date is <?php echo date("D, M jS, Y g:i a", strtotime($record['reception_date'])) ?> <br /> <?php endforeach ?> <hr /> </body> </html> <!– Start of second list viewer code–> <?php header('Content-type: text/html; charset=utf-8'); ?> <?php /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */ require_once "/hsphere/local/home/apbcweb/artistsofpalmbeachcounty.org/cmsAdmin/lib/viewer_functions.php"; list($date_compare_testRecords, $date_compare_testMetaData) = getRecords(array( 'tableName' => 'date_compare_test', 'where' => 'reception_date <= TIMESTAMP(NOW()) OR reception_date >= TIMESTAMP(NOW()+ INTERVAL 7 DAY)', )); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> EVENTS WITH RECEPTIONS BEFORE TODAY OR MORE THAN 7 DAYS IN THE FUTURE<br /><br /> <?php foreach ($date_compare_testRecords as $record): ?> Title: <?php echo $record['title'] ?><br/> The Reception Date is <?php echo date("D, M jS, Y g:i a", strtotime($record['reception_date'])) ?> <br /> <?php endforeach ?> </body> </html> Dave explained that for date queries, “The first step is to figure out if you need to do it in MySQL or PHP. If you are able to do it in MySQL it's often simpler. You can find a list of MySQL date/time functions here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html And here are some examples:
NOW() - Gives you the current date and time: 2010-03-01 09:41:50 (NOW() + INTERVAL 7 DAY) - Get date/time 7 days from now: 2010-02-22 09:45:25 CURDATE() - Gives you the current date only: 2010-03-01 TIMESTAMP() - Format value as date/time, or if two values, add them together TIMESTAMP(CURDATE()) - Get date/time at beginning of day: 2010-03-01 00:00:00 TIMESTAMP(CURDATE(), “23:59:59") - Get date/time at end of day 2010-03-01 23:59:59 You can test all these with the MySQL Console Plugin by entering SELECT followed by the function. So: SELECT NOW() in the Mysql Console returns: 2010-03-01 09:41:50 So the first step is to figure out the values you want to compare. My guess is you want: The date 7 days from now: (NOW() + INTERVAL 7 DAY) The reception date: reception_date The date at the end of today: TIMESTAMP(CURDATE(), “23:59:59") If you write it out in English first it's way easier: - If the reception_date is 7 days or less from now - AND the reception_date hasn't already passed I like to arrange my code so if reads like a time range with the test date in the middle like this: start_date >= test_date AND test_date >= end_date So that would be:
(NOW() + INTERVAL 7 DAY) >= reception_date AND reception_date >= TIMESTAMP(CURDATE(), “00:00:00") Now I understand why date math makes Dave’s head hurt. The first CMS Builder reference book is now available on-line! http://www.thecmsbcookbook.com
(This post was edited by gkornbluth on Mar 7, 2010, 5:09 PM)
|