If date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

If date

Post by jamrop »

Hi

I have a database that has a list of films record that a member puts in. When they add the record a date is stored in the database. Now want i am trying to do is, display the films a member has added, but if a record is a week old to display a button

so e.g.

Code: Select all

<?php
if (TODAY(now()) - (TODAY($date)) <= 7){



		echo "<td width="5%"  bgcolor="$row_color" valign="middle" align="middle">



		<a href='delete.php?advert_id=$advert_id&check=$member_id&type=ad '><img src='../classfied/cancel.gif' width="30" height="23" border="0"></a> </td>\n";}


?>
$date = the date a member added a film

I know that the if statement is wrong, but how would i go around so it understands the date now, and the date the film record was stored,.

Many thanks

jamie
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

e.g.

Code: Select all

<?php
$now = time();

$dummyRecordset = array(
		"20 September 2003",
		"19 September 2003",
		"1 September 2003"
	);
	
foreach($dummyRecordset as $row)
{
	$date = strtotime($row);	
	if ($now - $date <= (7*86400)) // a day has ~86400 seconds
		echo '!'; 
	else
		echo "#";
		
	echo $row, "  \n";
}
?>
if your databse supports it and you want to stay with that database you can let it do the work for you, e.g. for mysql you might use something like

Code: Select all

SELECT ...fields you need..., (insertDate &lt; Now() - Interval 7 day ) as oldMovie FROM movies
The records returned will have a field called oldMovie. If it's value is 0 then it's not an old movie, otherwise it is.

Code: Select all

while( ...fetching a row... )
...
    if($row['oldmovie'])  echo 'this is an old movie entry';
...
see also: http://www.mysql.com/doc/en/Date_and_ti ... tions.html
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

hey

Thanks for your help, but i am a bit confused of the sql statement you put.

At the moment i have

Code: Select all

select * from movies where member_id = '$member_id' and valid = 1;
that displays all the records

I dont understand where the

Code: Select all

(insertDate < Now() - Interval 7 day ) as oldMovie FROM movies
would go, cause if i put it as

Code: Select all

and insertdate< now() .......
after the valid = 1, it would only display the records that are over a week old, where as i need all the records.

Many thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

select *, (insertdate < Now() - Interval 7 day ) as oldMovie from movies where member_id = '$member_id' and valid = 1;
The resultset contains what you select: a set of records/rows each having the fields you've selected.

Code: Select all

select  *
means: from each recordset select all fields

Code: Select all

select  member_id
means: from each recordset select only the field member_id

Code: Select all

select  member_id, Now()
means: from each recordset select only the field member_id and the value of the function Now()

Code: Select all

select *, (insertdate < Now() - Interval 7 day )
means: from each recordset select all fields and the result of the comparison

Code: Select all

select *, (insertdate < Now() - Interval 7 day ) as oldMovie
means: from each recordset select all fields and the result of the comparison and name this field oldMovie
...

The where-clause determines wether a complete row is in the resultset or not
Post Reply