What PHP code can I use to stop showing text at a certain time?
For example: "Come to this event at 3PM on Monday!"
I don't want that to show after 3:01PM on the site.
What PHP code can I use to stop showing text at a certain time?
For example: "Come to this event at 3PM on Monday!"
I don't want that to show after 3:01PM on the site.
<?PHP
$when = strtotime('April 26, 2010 15:00:00');
if ($when > time()){
echo "Be there at 3pm on monday or be square";
}
$endTime = 1272319200; // The timestamp you want the message to stop displaying
if ( time() < $endTime){
echo "Come to this event at 3PM on Monday!";
}
else{
echo 'It\'s too late to show this message.';
}
First, you have to know the date and time of Monday 3pm, in a format that can be understood by PHP functions : 2010-04-26 15:00:00
Then, you have to convert that to an UNIX timestamp, which can be done with the strtotime() function :
$tsMonday = strtotime(2010-04-26 15:00:00);
After that, the time() function gives you the current timestamp ; which means you can use it to determine whether monday 3pm has passed or not :
if (time() < $tsMonday) {
echo "not yet monday 3pm";
}
Note : instead of converting the date to a timestamp with strtotime each time the page is called, you could directly put the timestamp value in your code -- if this is only for one event, it'll work great (but, for several events, it might become harder to maintain).