COMPARING DATES - Apr 1st, 2018


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 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>




The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service