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.
$expiration) {
echo "style='display:none;' ";
}
}
echo date("Y-m-d H:i:s");
?>
Check Expiration PHP Snippet
>
Save up to 20% until
Don’t miss this great sale on all styles of men’s and women’s clothing, but only until 00/00/00.
Hello! I thought that this snippet of code would be useful, but I seem to be having an issue with it. Can you please help me out?
When I load the PHP function on the page (after setting up the expiration-to-be div, I get this result:
Parse error: syntax error, unexpected $end in … on line 52 (which there is no line 52)
If I remove the PHP snippet, then I get this error:
Fatal error: Call to undefined function checkExpiration() in … on line 33
Therefore, I am confused as to how the function can cause a problem for the page to load, but then when I remove the function altogether, the page loads but is looking for it. 🙁
Thanks for your support!
__
Nikki
Nikki – I’ve cleaned up the PHP code. Perhaps you can give it another try.
Thanks Mike
I’m an old newbie trying to learn new tricks. I found this snippet to be an excellent piece and it worked great.
Thanks
George
Glad you found it useful, George.