Jump to content


Photo

Displaying only future dates in calendar


  • Please log in to reply
3 replies to this topic

#1 dooper3

dooper3
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts
  • LocationLondon, UK

Posted 11 May 2006 - 01:28 PM

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

<?
$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();
?>

Thanks everyone!!
Personal: charliehawker.com
Educational: fantasticode.com

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 12 May 2006 - 08:40 PM

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
$currentdate = mktime(0,0,0,date('m'),date('d'),date('Y'));

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 12 May 2006 - 08:44 PM

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

$today = time();

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

?>


PHP Ninja

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 12 May 2006 - 09:24 PM

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).
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users