Jump to content

Displaying only future dates in calendar


dooper3

Recommended Posts

Hi,

I'm having trouble getting my events calendar up and running. Basically, it's for a sports club and the committee can add dates to the calendar (all works fine) and the webmaster can remove them individually (again fine), but when I want to display them, I only want to display the events that have not yet happened (and are not happening today).

This is where I'm stumped, the way I want it to work is the PHP connects to a MySQL server and looks for dates that are after today's date, then chooses those events only to display but as I don't understand unix timestamps very well, I feel I am going about it the wrong way (with little success!)

Another issue I want to resolve is I want it to only display the date once for the viewer if there are multiple entries with the same date.

Below is my code so far...

[code]<?
$currentdate = mktime(0,0,0,date("j n Y"));
$sql = mysql_query("SELECT * FROM calendar WHERE date<='$currentdate' ORDER BY date ASC")
    or die(mysql_error());
$rows = mysql_numrows($sql);
if ($rows == "0") {
    echo("<b>Nothing in the Calendar</b>");
} else {
    while ($item = mysql_fetch_array($sql)) {
        extract($item);
        $eventdate = date("D jS F",$date);
        echo("<a name=\"$id\"><h4>$eventdate</h4></a><b><i>$event</i></b><br>
        $description<p>");
    }
}
mysql_close();
?>[/code]

Thanks everyone!!
Link to comment
https://forums.phpfreaks.com/topic/9546-displaying-only-future-dates-in-calendar/
Share on other sites

Use unformatted timestamps when just dealing with the backend stuff (Queries, storing in db, comparing dates, writing conditional statements,...etc). Make life much easier for you. Only format dates for display purposes.

So, what you would do to retrieve anything past today's date...you would do something extremely simple like:
[code]
<?php

$today = time();

$query_events = mysql_query("SELECT * FROM events WHERE date > '$today'");

?>[/code]

Caesar, you've misunderstood the mktime part. time() returns the current timestamp, but it is the "current" time. If you need to get the timestamp for "today" (meaning the START of today, at 0:0 am), you have to either use mktime or the MYSQL function "CURDATE". And mktime by no means format the date. It returns a timestamp based on the given arguments.

It could be an advantage to use DATE, DATETIME, and TIMESTAMP datatype in mysql, because mysql date and time functions are based on them. Also note that TIMESTAMP and UNIX_TIMESTAMP is different in mysql. TIMESTAMP has the format YYYYMMDDHHMMSS while UNIX_TIMESTAMP is the seconds since the epoch (1970).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.