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
Inspiring
August 29, 2007
Two Oaks Jerseys wrote:
> 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.

The best way to do that is by creating a SQL query that gets precisely
the information you want. Something like this should do it:

SELECT ConcertDate, Piece1, Piece2, Piece3
FROM table_name
WHERE ConcertDate >= CURDATE()
ORDER BY ConcertDate ASC
LIMIT 1

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
August 30, 2007
Yes, David, thank you. The SQL statement is exactly what I would do but I'm not sure how to place that inside my code. Do I call it as a variable? how do I enclose that statement into php? Right now the php statement does exactly the right thing, but that is of course because the database displays the first record. In order to stay current I'd have change the database as each date passes. The SQL statement would take care of that.. Just unclear of the syntax that calls it.
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/