PDA

View Full Version : Adding a date sensor to a php file?


Andy92
07-29-2006, 10:23 PM
Hi there,

On my site admin panel i have an adverts section where there is a php script in that page and it gets data from a mysql database called adverts and displays it in the adverts.php file. Also it orders it by what day and month each customers advertising package is due to expire.

Here is the php script i have in my page...

<?php
mysql_connect("localhost", "web15-mainsite", "passwordnewass") or die(mysql_error());
mysql_select_db("web15-mainsite") or die(mysql_error());
$result = mysql_query("SELECT * FROM adverts ORDER BY mn ASC, finday ASC");
if ($row2 = mysql_fetch_array($result)) {
do {
Printf("<tr><td width='157'><div align='left'>%s</div></td>
<td width='159'><div align='left'>%s</div></td>
<td width='126'><div align='left'>%s</div></td>
<td width='138'><div align='left'>%s</div></td></tr>", $row2[name],$row2[package], $row2[start], $row2[finish]);
}while ($row2 = mysql_fetch_array($result));
}else{
echo "No Adverts at the moment";
}
?>

Basically, could someone tell me how to add to the script, so that it would still order it by the date due to each advertising package expires, but the script would sense the present date, and then if the customers advertising package has expired then it would display the 'finish' date text in red and bold so then i will be able to know if it has expired?

Please help? :confused:

Fumigator
07-29-2006, 10:35 PM
You can use the mktime() function to compare timestamps.

You've made things harder than they need to be by not storing the expiration date as a date or a timestamp in your database, but it's still possible.

Here are the details:
http://us2.php.net/manual/en/function.mktime.php

Andy92
07-29-2006, 10:45 PM
Ok, so if i put a new field in my database called expire and the i put something like 23/7/2006 then in my php file i could put...

ORDER BY expire ASC

Will that work?

Andy92
07-29-2006, 10:49 PM
I have done it and it works,

but now how do i get it to sense the date and if it has expired make it highlight in red?

Fumigator
07-30-2006, 03:10 PM
That's what you use the mktime() function for. You want to compare the unix timestamp of "right now" against the unix timestamp of the timestamp you pulled off the database, and if the "right now" timestamp is less than the database timestamp then you know it has expired.

Andy92
07-30-2006, 07:29 PM
Thanks for all your help.

I have it sussed now and its all working!

:thumbsup: