Date Expired Problem

Hi
I have 2 tables in my database startdate and enddate, when the enddate has passed the startdate i want to update my database but can not get this to output the correct data, here is the code im using.

Here is my query

$req = mysql_query("select * from offers where price>0 and enddate < startdate and shopname='" . $_GET['shopname'] . "' ");
while($row= mysql_fetch_array($req))
{
	
?>
  </span>
</p>
<table width="1157" height="70" align="center" class="alert" id="main2">
  <tr>
    <td width="7" height="64">&nbsp;</td>
    <td width="1131"><ul>
      <table width="987">
        <tr>
          <td width="72" height="125"><img src="../app/images/products/<?php echo $row['image']  ?>" alt="" class="caption"  style="width:50px;  margin-right:6px" /></td>
          <td width="903"><table width="96%" height="46" class="table-bordered">
            <tr>
              <td height="19" class="alert-danger">Offer name</td>
              <td class="alert-danger">Start Date</td>
              <td class="alert-danger">End Date</td>
              <td class="alert-danger">Price</td>
              <td class="alert-danger">Expired</td>
              <td class="alert-danger">&nbsp;</td>
              </tr>
            <tr class="alert-info" id="result">
              <td width="209" height="19" class="alert-info"><?php echo $row['name']  ?></td>
              <td width="128" class="alert-info"><?php echo $row['startdate']  ?></td>
              <td width="111"class="alert-info"><?php echo $row['enddate']  ?></td>
              <td width="117"class="alert-info"><?php echo $row['price']  ?></td>
              <td width="135"class="alert-info">
			  <?php if ( 'startdate' > 'enddate'){
				  echo "Offer Expired";
                  }
				  else if
                    ( 'startdate' < '$enddate'){
					    echo "Offer Live";
                     }  ?></td>
              <td width="141"class="alert-info"><a href="profile.php?ID=<?php echo $row['ID']; ?>" class="large button green">Reset Offer</a></span></td>
              </tr>
          </table> 

if i echo $row[‘startdate’] and endate i get this format Sunday January 26, 2014 that’s what i wont but it doesn’t work

IN the 2 tables in the database field i use varchar for startdate and the same for enddate do i need to change these to datetime
thanks


if ( 'startdate' > 'enddate'){ 

// ... and ...

( 'startdate' < '$enddate'){

You’re comparing strings there, not variables. So that’s the first problem.

Secondly, when you do put the variables there, you will still be comparing strings which will not work the way you want. If you’re storing the dates as strings, use strtotime() to convert them to timestamps so you can compare them.

Actually, the dates from MySQL database are in YYYY-MM-DD format so you can compare then as strings in PHP because in this case alphabetical order is the same as the date order in the calendar. The problem seems to be the lack of proper variable declarations so the code should look like:


if ($startdate > $enddate){ 

// ... and ...

( $startdate < $enddate){

Variables must start with $ and not be enclosed in single quotes. EDIT: I just noticed QMonkey spotted this problem, too!

I took this to mean they’re stored in that format (in a varchar field probably). If so, they will have to be converted.

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.