Different text based on date (php)
The following PHP code snipplet is useful for displaying different text depending on the date. This is particularly useful for time limited specials and promotions where you can set up your special or promotion and its expiration date, then forget about it.
The PHP code
Add the following code at the top of your web page. This will set the variable $buffer, which contains the text string you wish to print out depending on the date.
<?php
$buffer = '';
$expiry_date = mktime(0,0,0,12,31,2005);
if ($expiry_date > time()) {
$buffer = 'This string will appear before the date December 31, 2005.';
} else {
$buffer = 'This string will appear after the date December 31, 2005.';
}
?>
Explanation
Line 1: Start PHP.
Line 2: Set $buffer variable as empty (creates the variable $buffer).
Line 3: Set the expiry date variable, $expiry_date, this is in the format of mktime(hour, minute, second, month, day, year) where each time is a integer.
Line 4: If the the date (time()) is before the expiry date ($expiry_date), then…
Line 5: Set the variable, $buffer, with the text string “This string will appear before the date December 31, 2005.”
Line 6: Else, if the date is after the expiry date, then…
Line 7: Set the variable, $buffer, with the text string “This string will appear after the date December 31, 2005.”
Line 8: Closes the If
Line 9: End of PHP
Calling the variable
In the HTML or XHTML section of your web page, use the following code where you would like to display the variable’s, that is, $buffer’s text string.
<?=$buffer?>

