I maintain several websites that can contain time-sensitive information. Often times I’ll post something that should appear until a certain future date and time. In the past, I’ve set a calendar reminder to prompt me to remove the information from the website. But sometimes I’d get busy and I’d forget!

A simple solution was to write this little snippet of PHP code to automatically ‘hide’ a portion of a web page a the appropriate time.

This PHP function inserts the CSS style attribute of display:none, which hides the respective HTML, plus it doesn’t take up any space on the page.

Here’s how you apply it. Just insert the checkExpiration function on a wrapping tag with the date and time stamp formatted as shown. In this example, the div will appear until 12:00:00 p.m. on April 17, 2015. After that, it will automatically disappear.

<?php  
 
#  automatically turn off announcements based on a specific date/time
function checkExpiration($expiration) {
   $now = date("Y-m-d H:i:s");
   		if ($now > $expiration) {
   		echo "style='display:none;' ";
   }
}
 
echo date("Y-m-d H:i:s"); 
 
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Check Expiration PHP Snippet</title>
</head>
<body>
 
<div <?php checkExpiration("2015-04-17 10:37:00"); ?> >
   <h3>Save up to 20% until</h3>
   <p>Don’t miss this great sale on all styles of men’s and women’s clothing, but only until 00/00/00.</p>
</div>
 
</body>
</html>