SL8R.co.uk - Code Zone

Code, Code and Code

SL8R.co.uk - Code Zone header image 2

PHP: Format and show an expiration date

April 8th, 2008 · No Comments

A useful routine to calculate and show an expiration date based on a given number of hours

// Hours to expiration
$iHrs = 72;
// Date from database
$dbDate = ‘2008-04-08 16:53:19′;
// obtain current date and time
$iTimeNow = date(”U”);
// calculate expiry time
$iExpiryTime = ($iHrs * 3600) + strtotime(”$dbDate”) ;
// set the message to display
$strMessage = “”;
// compare times
if ($iTimeNow < $iExpiryTime)
{
  $strMessage = “Expiry time has been reached!’;
}
else
{
  // compute the difference in times
  $iTimeLeft = $iExpiryTime - $iTimeNow;
  // breakdown the results
  $days = floor($iTimeLeft / 86400);
  $hours = floor(($iTimeLeft - $days * 86400) / 3600);
  $mins = floor(($iTimeLeft - $days * 86400 - $hours * 3600) / 60);
  $secs = floor($iTimeLeft - $days * 86400 - $hours * 3600 - $mins * 60);
  // show the results
  $strMessage = ‘Time to expiry: ‘ . $days . ‘ days ‘ . $hours . ‘ hours ‘ . $mins . ‘ min and ‘ . $secs . ‘ seconds’;
}
echo $strMessage;
?>

Tags: Code

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment