Compare dates

17 posts by 4 authors in: Forums > CMS Builder
Last Post: January 27, 2010   (RSS)

By gkornbluth - May 31, 2009

Hi all.,

I’m comparing the date in a date field called opening_reception to the current date and am almost there.

I’m stuck on the “elseif” statement. I want it to check to see if the opening_reception date and today's date are equal.

When I set the opening_reception date to today’s date I expect it to be caught by the "elseif" statement but I still get a result that matches the “else” statement.

I’m pretty sure that it’s a simple syntax thing, but I can’t figure it out.

Hope someone has the answer...

Thanks,

Jerry Kornbluth


<?php foreach ($exhibitionsRecords as $record): ?>

<?php $today = date("U", strtotime("now")); ?>
<?php $date = date("U", strtotime($record['reception_date'])); ?>

<?php if ($date > $today): ?>
Opening Reception: <?php echo date("l, F jS", strtotime($record['reception_date'])) ?>

<?php elseif ($date == $today): ?>
The Opening reception is today. Don’t miss it!!

<?php else: ?>
Sorry, you missed the Opening Reception.

<?php endif; ?>
<?php endforeach ?>
The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Compare dates

By ross - June 1, 2009

Hi Jerry.

Thanks for posting!

The best way to start trouble shoot this one would be to just echo out $today and $date right before doing any of the if/else stuff. That way you can see what they are being set as.

From there, it will be much easier to sort out. Let me know how it looks. Thanks!
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Compare dates

By gkornbluth - June 1, 2009

Thanks for the great idea, Ross. Didn't think of that.

I also added "U D, M jS, Y g:i:s a" to the date format to see exactly what was going on.

When I set the opening_reception date to June 1, instead of a match here's what I get.

The opening reception date: 1243814400 Mon, Jun 1st, 2009 12:00:00 am

Today's Date: 1243855016 Mon, Jun 1st, 2009 7:16:56 am

So it'd clear that the dates will only match for a 1 second duration at midnight... (not very helpful)

How can I limit the comparison to only the Month Day and Year and not compare the hours minutes and seconds? Or would you suggest another approach.


Thanks,

Jerry
The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Compare dates

By Dave - June 1, 2009

Hi Jerry,

Great job, you got right to the root of the problem. From there, sometimes it helps to break the problem down into smaller pieces, write those out, solve them one by one, and then put the code back together (and I like to add the step of trying to make it look nice).

Try this:

<?php foreach ($exhibitionsRecords as $record): ?>
<?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);
$isEventOver = !$isEventToday && ($eventUnixTime < $currentUnixTime);
$isFutureEvent = !$isEventOver && !$isEventToday;
?>

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

<?php endforeach ?>


An added benefit of assigning your tests to variable names with descriptive english names is it makes the code much easier (and english-like) to read.

Hope that helps, let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [gkornbluth] Compare dates

By gkornbluth - June 2, 2009

Worked Perfectly.

Thanks again Dave
The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Dave] Compare dates

By gkornbluth - January 12, 2010

Hi Dave,

It's probably my lack of understanding of how date strings and arithmetic work together, but I need your help (again)

I'm using the code above to group future events and past events in a list viewer.

The way I'm currently implementing it, an event becomes a past event as soon as the event date matches the current date.

What I really need to do is adjust the code so that an event does not become a past event until the day after the event date.

Here's the way I'm using the code above in 2 separate foreach loops.

<?php if ($isFutureEvent): ?> ...Show Upcoming Events... <?php endif; ?>

and

<?php if ($isPastEvent): ?> ...Show Past Events... <?php endif; ?>

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Compare dates

By Chris - January 12, 2010

Hi Jerry,

Can you please attach the complete PHP source code for your page?
All the best,
Chris

Re: [chris] Compare dates

By gkornbluth - January 12, 2010 - edited: January 27, 2010

Thanks Cris,

Here you go...

Jerry

Top of Page

<?php

require_once "/hsphere/local/home/apbcweb/artistsofpalmbeachcounty.org/cmsAdmin/lib/viewer_functions.php";

list($general_meetingsRecords, $general_meetingsMetaData) = getRecords(array(
'tableName' => 'general_meetings',
));


?>


Body...

<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 ?>
The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Compare dates

By Chris - January 12, 2010

The way I'm currently implementing it, an event becomes a past event as soon as the event date matches the current date.

What I really need to do is adjust the code so that an event does not become a past event until the day after the event date.


$isEventOver should never be true if the event's day is the current day ($isEventToday.)

$isEventOver = !$isEventToday && ($eventUnixTime < $currentUnixTime);

Looking at your code, I would imagine that events which occur on the current day would not be displayed in either list.

Are you saying that events which occur on the current day are showing up in your bottom list?
All the best,
Chris