Skip to main content
Participant
August 10, 2007
Question

conditional behaviour on date

  • August 10, 2007
  • 5 replies
  • 687 views
I'm trying to set a 'show if conditional' behaviour based on the current date compared to the date in the database to determine if a link should show/not show - I don't think I'm using the date() correctly to find the current date - can anyone shed some light on this for me?

<?php
// Show IF Conditional region1
if (@$row_rs_open_mtg['date_agenda_mtg'] <= "date()") {
?>
<?php }
// endif Conditional region1
?>

Thanks, Bob
This topic has been closed for replies.

5 replies

Inspiring
August 14, 2007
.oO(taluswood)

>Thanks David,
>I got this to work:
>if (strtotime(@$row_rs_open_mtg['date_agenda_mtg'] ) >= mktime(0,0,0)) {
>
>and . . . yes it is more efficent !

It could be even more efficient to let the database do that kind of
work:

SELECT
IF(date_agenda_mtg >= CURDATE(), 1, 0) AS validDate,
...
FROM
...

Then in PHP:

if ($row_rs_open_mtg['validDate']) {
...
}

Micha
August 29, 2007
This is precisely what I'm trying to do except I don't understand how to code that parameter.
I have a record set called rs_ConcertNext
I want to show a record based on whether the date in that record set is greater than or equal to today.
Right now my record set just shows the first of many records. I don't want to repeat region here, just display the record that is greater than yesterday's date. Yes, in the MYSql database, my date is formated yyyy-mm-dd.
Thanks for any help.

taluswoodAuthor
Participant
August 14, 2007
Thanks David,
I got this to work:
if (strtotime(@$row_rs_open_mtg['date_agenda_mtg'] ) >= mktime(0,0,0)) {

and . . . yes it is more efficent !

cheers, Bob
Inspiring
August 10, 2007
On Fri, 10 Aug 2007 12:21:29 -0500, Gary White <reply@newsgroup.please>
wrote:

>efficent

I'm so into efficiency that I've started conserving letters.

efficent can then be read as efficient.

Gary
Inspiring
August 10, 2007
On Fri, 10 Aug 2007 16:05:33 +0100, David Powers <david@example.com>
wrote:

><?php
>if (strtotime($row_rs_open_mtg['date_agenda_mtg']) <= mktime(0,0,0)) {
> // the date is earlier than today
> }
>?>
>
>To compare the two values, you need to convert them to Unix timestamps.
>strtotime() does that with a MySQL date, setting the time to midnight.
>mktime(0,0,0) generates midnight at the start of the current day.

Might be more efficient to just use the date function to format the date
as a MySQL date string:

if (@$row_rs_open_mtg['date_agenda_mtg'] <= date('Y-m-d')) {

However, it would probably be even more efficent to build it into the
query, so you wouldn't need the comparison here at all.

Gary
Inspiring
August 10, 2007
taluswood wrote:
> I'm trying to set a 'show if conditional' behaviour based on the current date
> compared to the date in the database to determine if a link should show/not
> show - I don't think I'm using the date() correctly to find the current date -
> can anyone shed some light on this for me?

The PHP date() function is used to format a date. How you create a
conditional statement controlled by the current date depends on how the
date is stored in the database. Since you're using PHP, I assume that
the date is stored in MySQL in the standard MySQL format of YYYY-MM-DD.
If that is the case, change this:

> <?php
> // Show IF Conditional region1
> if (@$row_rs_open_mtg['date_agenda_mtg'] <= "date()") {
> ?>
> <?php }
> // endif Conditional region1
> ?>

to this:

<?php
if (strtotime($row_rs_open_mtg['date_agenda_mtg']) <= mktime(0,0,0)) {
// the date is earlier than today
}
?>

To compare the two values, you need to convert them to Unix timestamps.
strtotime() does that with a MySQL date, setting the time to midnight.
mktime(0,0,0) generates midnight at the start of the current day.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/