Compare dates

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

I tend to agree, but yes, events which occur on the current day are showing up in your bottom list. (I just checked again)

Do you think it could it have something to do with the way I'm formatting the dates when I define

$eventDateString = date("l, F jS", $eventUnixTime);

and

$currentDateString = date("l, F jS", $currentUnixTime);

?

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 Chris - January 13, 2010 - edited: January 13, 2010

I tested your code and was unable to reproduce the problem. I created a general_meetings section with two fields: date and content; I added 5 records: one with the current date, one for yesterday, one for tomorrow, one for last year, and one for next year. Your viewer code is displaying both future meetings in the top list, both past meetings in the bottom list, and is not displaying the meeting with today's date at all. Exactly as expected.

You're having a problem in which a meeting for today is showing up in your "past meetings" list. I wonder if this could this be a timezone issue -- is your "today" the same as your server's "today"?

Let's make sure all the variables are what we expect them to be; add the code in red to your bottom list (you can comment out the top list for now to make your output easier to follow if you want):

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

Testing event:
<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>
</ul>


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


If you have an event which occurs on the current day, it should show the same date as the current day and "$isEventToday = true".

Please post your findings and we'll get to the bottom of this.
All the best,
Chris
Chris,

Thanks for this.

I'm not ignoring you but I need to finish some stuff before I can get back to it.

I'll let you know what I find.

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 Chris - January 14, 2010

Hi Jerry,

No worries. Take your time. I'm subscribed to the thread, so I'll see when you respond, whether it's in 2 days or 2 months. :)
All the best,
Chris

Re: [gkornbluth] Compare dates

By Dave - January 27, 2010

Hi Jerry,

Is your timezone set correctly under: Admin > General > Regional Settings?

Date math is notoriously difficult, for this and many other reasons.

If this is still an issue, and the timezone is set correctly then we'll have to go line by line and figure out which line is giving us the wrong date.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Compare dates

Hi Dave,

As I mentioned in my last post, everything seems to be working as expected.

Thank you all for your help.

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