PDA

View Full Version : need php/mysql help, p p pleeease...


Hyde
12-18-2001, 02:26 AM
Kind of hard to explain for a newbie at php/mysql like me, but I hope some of you get it... :)

I have a MySQL database where all posts have a datetime field.
How can I display all posts that have the date set to today or later?
(In other words: I don't want to display posts with older dates than today)


Database example:

test1 2001-12-17 00:00:00
test2 2001-12-18 00:00:00
test3 2001-12-19 00:00:00


I only want to display 'test2' and 'test3' but not 'test1'

Is there a proper SQL query for this or is additional coding necessary?

This is the code I have now (that shows all posts):

$data = mysql_db_query($database, "SELECT * FROM table1 ORDER BY date");

while ($row = mysql_fetch_array ($data)) {

print $row["date"];
print "<BR>";
print $row["info"];
print "<BR><BR>";

}

Backov
12-18-2001, 02:59 AM
First off, don't use mysql's "datetime" field, use integer and the standard unix timestamp you get from the php time() call.. This just simplified your life immensely. time() returns the time in seconds since jan 1,1970 (iirc)

86400 seconds is 24 hours. 3600 seconds is 1 hour. You can use the various PHP date/time functions to get the target date you need and then feed it to your query as an integer. You'll find after a while that working with these integer dates becomes second nature.

Cheers,
Backov

High Roller
12-18-2001, 04:34 AM
if you still need help
try sales@axehost.com
he could do some work for ya

eXtremal
12-18-2001, 07:07 AM
select * from table1 where date>=$today_date order by date;


this should be enough as long as the field is of type "date" or "timestamp".

Hyde
12-18-2001, 08:25 AM
Thanks for your reply Backov,
Maybe I will try that in my next script :)


I got it working now, with eXtremal's help.
Thanks!!

horse
12-18-2001, 12:44 PM
Originally posted by Backov:
<STRONG>First off, don't use mysql's "datetime" field, use integer and the standard unix timestamp you get from the php time() call.. This just simplified your life immensely. time() returns the time in seconds since jan 1,1970 (iirc)

86400 seconds is 24 hours. 3600 seconds is 1 hour. You can use the various PHP date/time functions to get the target date you need and then feed it to your query as an integer. You'll find after a while that working with these integer dates becomes second nature.

Cheers,
Backov</STRONG>


Backov, steering away from using MySQL's built in time storage is a bad idea IMHO. If you need the Unix timestamp that bad, just do the following on a datetime field:
SELECT UNIX_TIMESTAMP( datetime_field ) FROM blah
.. and you'll get a timestamp in return. Since you use datetime fields you can order by time easily within MySQL.

:)
Atle

Carneal
12-18-2001, 02:22 PM
$query = "Select * FROM what ORDER by date DESC";

will line up the order of what from todays date on to the 1st post

if u take off DESC it will line up the what from the 1st record to the last