Jump to content


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


Displaying only future dates in calendar

Recommended Posts


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

$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)) {
        $eventdate = date("D jS F",$date);
        echo("<a name=\"$id\"><h4>$eventdate</h4></a><b><i>$event</i></b><br>

Thanks everyone!!

Share this post

Link to post
Share on other sites
what's the data type of column `date` (DATE, DATETIME, TIMESTAMP, UNIX_TIMESTAMP)?

also, your usage of mktime is incorrect. this function only take integers. you're looking for this
[code]$currentdate = mktime(0,0,0,date('m'),date('d'),date('Y'));[/code]

Share this post

Link to post
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:

$today = time();

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


Share this post

Link to post
Share on other sites
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).

Share this post

Link to post
Share on other sites


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.