Jump to content

The forums are in maintenance (read-only) mode until further notice. If you search these forums, you may find the answer you need. Otherwise, Google and Stack Overflow are reliable resources; arguably faster ones, too. I apologize for any inconvenience. (For those concerned--and thanks for being so thoughtful, there's no cause for alarm, but after 12+ years running these forums in my "spare time", I'm opting for a much deserved break to recharge my depleted batteries.)


Expiry Date (Repeat)

  • Please log in to reply
3 replies to this topic

#1 alex



  • Members
  • Pip
  • 6 posts

Posted 24 July 2011 - 4:06 AM

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

$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>';

  • 0

#2 Stuart


    Advanced Member

  • Members
  • PipPipPip
  • 142 posts
  • LocationBirmingham, UK

Posted 24 July 2011 - 10:07 AM

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.
  • 0

#3 alex



  • Members
  • Pip
  • 6 posts

Posted 10 August 2011 - 4:08 AM

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
  • 0

#4 Larry



  • Administrators
  • 4,399 posts
  • LocationState College, PA (USA)

Posted 10 August 2011 - 9:03 AM

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.
  • 0