Only show image if expire data??

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
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Only show image if expire data??

Post by jmansa »

I'm making this fairly simple script, but can't seem to make it work! All images has an expire date like this:

db field (expires)
2009-06-11 00:00:00

I then want to show the image if the date hasnt expired current date. This I try to achive like this:

Code: Select all

$time = date('d-m-Y');
 
$result = mysql_query("SELECT *,DATE_FORMAT(expires, '%d-%m%-%Y') AS expires FROM sponsor WHERE uid='".$uid."'");
while($row = mysql_fetch_array($result)) {
 
echo '<table width="320" border="0" cellspacing="0" cellpadding="0"><tr>';
if ($time >= $row['expires']) {
    echo '<td><div align="left" style="padding-top:5px;">Hits: '.$row['hits'].' | Expires: '.$row['expires'].' | current date: '.$time.'</div></td>'; }
    elseif ($time < $row['expires']) {
    echo '<td><div align="left" style="padding-top:5px;">Hits: '.$row['hits'].' | Expires: <r>EXPIRED!</r></div></td>'; }
  echo '</tr></table><br>';
 
}
My $time is (22-06-2008)

I then have 2 records in my db with expires dates like this:
Record 1. 11-06-2009
Record 2. 20-06-2008

Now I would like to make the script show the expires date on record 1 and "EXPIRED!" at record 2.

What happens now is that it shows both with the expires date?!?!?

If I use this operator "<=" it shows both as "EXPIRED!"?!?!

Where do I go wrong? Please help :-)
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: Only show image if expire data??

Post by vargadanis »

It seems to me that they are string which are kinda tricky to compare. If the time is stored in unix time than it is simple to compare them as it is only an integer. Try not to format the data and see what u get.
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Re: Only show image if expire data??

Post by jmansa »

Ok... Try to use mktime() instead af date('d-m-Y') and remove the formatting from the db.

Now my $time looks like this:
1214130153

And my time from my record looks like this:
2009-06-11 00:00:00

How do I compare those 2?
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Re: Only show image if expire data??

Post by jmansa »

Ok... Solved it by using this:

Code: Select all

 
$t=explode(' ', $row['expires']); 
$d=explode('-', $t[0]); 
$unix=mktime(0,0,0, $d[1], $d[2], $d[0]); 
 
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Only show image if expire data??

Post by Kieran Huggins »

strtotime() is one of the PHP functions I miss most in other languages.

Know it. Use it. Love it.
Post Reply