Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF event_date > $today

Status
Not open for further replies.

t5amec

Programmer
Aug 14, 2003
112
GB
Take a look at the code below:
Code:
<?
$today=date("d/m/Y");

echo "<table border='0' cellpadding='1' cellspacing='2' width='100%'>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
if ($row[event_date]>$today) {
  echo "<td>Future</td></tr>"; }
else {
  echo "<tr><td>Past</td></tr>"; }
  echo "<td id='gigsHeadline'>" . $row['event_name'] . "</td></tr><tr>";
  echo "<td id='gigsSynopsis'>''" . $row['tag_line'] . "''";
  echo $today;
  echo $row[event_date];
  echo "</td></tr>";
  }
echo "</table>";mysql_close($con);
?>

I think its quite obvious what I want to do, if the event date is greater than todays date, then it should say Future, and if not, it should display Past...

But at the moment all fields are displaying future, although there are some with 'event_date' for dates gone by

Please take a look at the code, and inform me if there is an error

Make Sense? I hope so (-:
 
it's easier to work with serial dates for comparisons.

the code below depends on $row['event_date'] being in a internationally acceptable format. best is yyyy-mm-dd

Code:
<?
$today=[red]time();[/red]
echo "<table border='0' cellpadding='1' cellspacing='2' width='100%'>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
 [red]$cTime = strtotime($row['event_date']);[/red]
if ([red]$cTime[/red]>$today) {
  echo "<td>Future</td></tr>"; }
else {
  echo "<tr><td>Past</td></tr>"; }
  echo "<td id='gigsHeadline'>" . $row['event_name'] . "</td></tr><tr>";
  echo "<td id='gigsSynopsis'>''" . $row['tag_line'] . "''";
  echo $today;
  echo $row[event_date];
  echo "</td></tr>";
  }
echo "</table>";mysql_close($con);
?>
 
one other thing, the following code:
Code:
$date=date("d/m/Y", $row['event_date']);
produces the result of 31/12/1969 although I none of the dates in the DB are that...

is this some sort of default date?
how do i get my date from the DB to display in d/m/Y format, rather than yyyy-mm-dd?

Make Sense? I hope so (-:
 
two things:

1. read the manual: date() takes a unix time stamp as a second argument. not a string.

2. databases tend to handle dates in their own way: most commonly using yyyy-mm-dd hh:mm:ss as the format. d/m/y is not an acceptable international format. you can store dates in that way but they will be treated as straight text and will not be manipulable.

you can format the OUTPUT of dates from mysql using the mysql built-in functions (e.g. dateformat) or you can handle dates programmatically in php using a combination of date(), mktime(), strtotime() etc.
 
Check out the date_diff of mysql, if that's what you use.
Code:
mysql> SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');
        -> 1
mysql> SELECT DATEDIFF('1997-11-30 23:59:59','1997-12-31');
        -> -31

ref.:
You can select the datediff AS datediff

Then you can check the row['datediff'], if it's <, > or == 0. (switch is good for this)

Olav Alexander Mjelde
Admin & Webmaster
 
so if i was to put in the coding:
Code:
$dateFormat = DATE_FORMAT($row['event_date'],'%W %M %Y')
it would produce the result 'Saturday October 1997' or whatever?

Make Sense? I hope so (-:
 
i dont think thats write, considering when i use that coding, i seem to lose all my db date on the page...

The format on the db is yyyy-mm-dd hh:mm:ss

any help, this really has got me stumped, haha

Make Sense? I hope so (-:
 
i misunderstood your intent

the date_format() is a MYSQL function, not a php function.

so your code would be

Code:
select DATE_FORMAT(event_date,'%W %M %Y') as event_date from table
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top