alex Posted July 24, 2011 Share Posted July 24, 2011 Hi all my question is about duo date by reference of this thread I read the MySQL's DATEDIFF(expr1,expr2) but still I dont know how it will be work. Mr. pswason told to use SELECT DATEDIFF(expiration_date, CURDATE()) WHERE id=$id; and he dont know about ... he says "I don't know if a card is considered expired if the expiration date is the current date" I give the current date as not expiration date so I make my script as: $tdy = date('Ymd'); in detail our many cards have expiry date like 19-08-2011, and we like to see how much days remaining by substract today date from. my whole code is <?php $timezone = new DateTimeZone( "Asia/Riyadh" ); $date = new DateTime(); $date->setTimezone( $timezone ); echo $today = $date->format( 'H:i:s A / D, M jS, Y' ); echo '<br />'; function datediff ($expri, $tdy) { $tdy = date('Ymd'); $tmr = (($expri - $tdy)/100*30); // $expri will be take from DB. return $tmr; } $query = 'SELECT * FROM staff'; $result = mysql_query ($query); while ($row = mysql_fetch_array ($result, MYSQL_ASSOC) ) { echo '<tr valign="top">'; echo '<td>' . $row['id'] . '</td> <td>'. $row['name'] . '</td><td align="center">' . $row['nationality'] . '</td><td align="center">' . $row['card_no']; '</td><td align="center">'; $exdate = 'SELECT DATEDIFF(card_expiry_date, CURDATE())';// I dont know how to use mysql's condtions in php. echo '</td><td align="center">' . datediff($expri = $row['card_expiry_date'], $tdy); echo '</tr>'; } mysql_free_result($result); mysql_close(); ?> Link to comment Share on other sites More sharing options...
Stuart Posted July 24, 2011 Share Posted July 24, 2011 Not tested but can't you just do this: $q = "SELECT DATEDIFF(card_expiry_date, CURDATE()) AS days_till_expiry FROM staff"; A negative value would indicate that the expiry date has been exceeded. Link to comment Share on other sites More sharing options...
alex Posted August 10, 2011 Author Share Posted August 10, 2011 solved! function datediff ($expri) { $now = time(); $diff = strtotime ($expri) - $now; // $expri will be take from DB. return floor($diff/86400); } the main thing was "floor" results thanks to all Link to comment Share on other sites More sharing options...
Larry Posted August 10, 2011 Share Posted August 10, 2011 Thanks for sharing the solution. I would recommend, however, that if the $expri is coming from the database, that all of the calculation takes place there, too. You can floor the results in SQL. Link to comment Share on other sites More sharing options...
Recommended Posts