Michael
Michael

Reputation: 1

Stop showing text at a certain time - PHP

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.

Upvotes: 0

Views: 452

Answers (3)

timdev
timdev

Reputation: 62914

<?PHP
    $when = strtotime('April 26, 2010 15:00:00');
    if ($when > time()){
       echo "Be there at 3pm on monday or be square";
    }

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 401172

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][1], which can be done with the [`strtotime()`][2] function :
$tsMonday = strtotime(2010-04-26 15:00:00);

After that, the [`time()`][3] 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)*.

Upvotes: 4

waiwai933
waiwai933

Reputation: 14579

$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.';
}

Upvotes: 0

Related Questions