Page 1 of 1

Only show image if expire data??

Posted: Sun Jun 22, 2008 5:04 am
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 :-)

Re: Only show image if expire data??

Posted: Sun Jun 22, 2008 5:07 am
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.

Re: Only show image if expire data??

Posted: Sun Jun 22, 2008 5:26 am
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?

Re: Only show image if expire data??

Posted: Sun Jun 22, 2008 6:10 am
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]); 
 

Re: Only show image if expire data??

Posted: Sun Jun 22, 2008 7:34 am
by Kieran Huggins
strtotime() is one of the PHP functions I miss most in other languages.

Know it. Use it. Love it.